March 2, 2015 · Programming Javascript

Associative arrays in Javascript

JavaScript does not support arrays with named indexes or in other words; Associative arrays is not built into JS. The only way to have an associative array (the easy way) is to use Javascript objects. This will allow you to simulate an associative array. I'm not sure about the performance hit of using a JS object as an associative array, since i've only been using it on a very small scale in test code, but its pretty useful. I would look out for the memory usage of a JS object.....

Anyways, here is an example below:

var associative = {};
associative["hello"] = "world";

console.log(associative["hello"]); //should print out "world"


  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket
Comments powered by Disqus