I am Hack Sparrow
Captain of the Internets.

Associative Arrays in JavaScript – There’s None!

JavaScript does not support Associative Array

That sounds strange, but it's true. JavaScript does not have associative arrays. An associative array is a kind of array which uses strings as indices, instead of numerical ones.

Take a look at the code below.

var hero = new Array();
hero['name'] = 'batman';
hero['id'] = 'Bruce Wayne';
hero['city'] = 'Gotham City';

for (var p in hero) {
    console.log(p+': '+hero[p]);


Isn't that how associative arrays are supposed to look like and work like? If it walks like a duck and quacks like a duck, it should be an associative array! I mean a duck.

Not so fast, grasshopper. Take a look at this code.

hero[0] = 'Joker';

True array objects should have a proper length property. That doesn't seem to be the case at all in the case above. JavaScript supports only numerically indexed arrays. The 'associative array' you saw was actually object properties assigned to the Array object.

In JavaScript everything, except primitive data types, are objects. And you can assign any property to almost any object at any point of time. Some object properties are read-only, you can't assign new values to them or delete them, eg: window.height.


  1. JavaScript doesn't support associative arrays.
  2. True arrays should have a valid length property.
  3. Everything in JavaScript are objects, except for primitive data types.
  4. Some object properties are read-only in JavaScript.


  1. Find a way to implement associative array in JavaScript.
  2. What are primitive data types?
  3. List some ready-only properties in a. Core JavaScript, b. Client-side JavaScript.

Now that you know there is no associative array in JavaScript, can you answer this question - can you use objects as keys in JavaScript?

5 Responses to “Associative Arrays in JavaScript – There’s None!”

  1. Tony says:

    Is an object the equivalent to an associative array ?

  2. Captain says:


  3. Tony says:

    been diggin into your blog, good stuff mate!

  4. Captain says:


  5. ferry says:

    you can still get the length by enumerating the object’s (own) keys and counting.

Make a Comment