Hash Tables in JavaScript

One of my C# code snippets had a Hash Table functionality and I was trying to emulate the same in JavaScript. JavaScript does not have a built-in HashTable, however you can make use of object properties as hash-type tables.

Here’s an example:

javascript-hashtable

OUTPUT: Jane

The limitation in the above method is that you can use only string or integers as keys. When you are using integers as keys, it gets converted to string while setting the property. If you want to use objects as keys, check this nice library called jshashtable

You can also write the code shown above in the following way:

javascript-hashtable-example

No comments:

Post a Comment