What is Associative Array in JavaScript?

In this post I discussed about Arrays in JavaScript. We access elements of JavaScript array using numeric index. For example, if you want to access 3rd element of a JavaScript array, you will do that as following


If you want to traverse through each element of the array you can do that in loop. JavaScript also allows you to store and access elements in the array in using string index than numeric index.


You can create Associative Array in JavaScript as following,


We are assigning values in array using string index. You can access a particular element with string index name. We can access name information from student information as following


One problem working with Associative Array is that we can not traverse through array elements in a loop. Associative Array can be best utilized with Array with Arrays is JavaScript. Read here about Array of Arrays

Let us take above example of student array. If we want more than one student to be stored then, we can create student array as following


Then we can create associative Student Array as following


Now if push student in arrayofStudent array.


If we want to access name of the first student from the array, we can do that as following,


For your reference code is given below we used in explaining Associative Array.

 <script type="text/javascript" >
 var arrayofStudent=[];
 var student1 = [];
 student1["rollnumber"] = 19;
 student1["name"] = "John";
 student1["grade"] = "A";
 var student2 = [];
 student2["rollnumber"] = 11;
 student2["name"] = "David";
 student2["grade"] = "B";

<span style="font-size: 12pt;"></span>


<span style="font-size: 12pt;"></span>

In this way you can work with Associative Array in JavaScript. I hope you find this post useful. Thanks for reading.Β 

6 thoughts on “What is Associative Array in JavaScript?

  1. Nice post & really beautifully explained. I got it now.

    I guess there is a small typo “… with Associative Array is that we can traverse through …” , may be it is “..can not..”.

    Thanks for the blog post !

  2. Hello Sir As much i have studied about array in JS, I think there is nothing like associative array in Javascript,
    In JavaScript, arrays always use numbered indexes. If you use named indexes, JavaScript will redefine the array to a standard object.

    please correct me if i am wrong sir

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s