Working with Array of Arrays in JavaScript


If you have worked with languages like C, C++ or C# then you must be aware with the concept of multidimensional array. Multidimensional array implies arrays of array. In a two dimensional array, there would be rows and columns. A 3*5 two dimensional array can be shown as below,

0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2
3,0 3,1 3,2
4,0 4,1 4,1

Even though you cannot declare a multidimensional array directly in JavaScript though you can have Arrays of Array in JavaScript.

You can define a two dimensional array as following in JavaScript



In above code

  • First we defined a base array
  • Then we created two chid arrays. These arrays will constitute different rows of the base array.
  • Each child array is of different size.
  • Assigning each chid array as one of the rows of base array. 

In last line of above code listing, we are printing value of 1st row and 4th column of base array. We will get output as following,

On noticing above code listing you will find that chid array is of different size. In JavaScript chid arrays can be of different size. Essentially you can have three dimensional array in JavaScript as well. You can define that as following code listing,


In above code listing,

  • We are defining array named grandparents. This is the base array.
  • We are defining array named parents. Grandparents can have parents.
  • We are defining array names child. Parents can have child.

We want to print “steve”. Steve is second element of child2 array. child2 array is second element of parents1 array. parents1 array is first element of grandparents array. so we can fetch steve with following index value on grandparents array


As output you will get steve printed as following,

For your reference code of two dimensional array and three dimensional array in JavaScript is given below,

<script type="text/javascript" >
 //Two Dimensional Array
 var baseArray = [];
 var chidArray1 = ["a", "b", "c", "d", "e"];
 var chidArray2 = ["1","2","3","4","5","6","7","8"];
 baseArray[0] = chidArray1;
 baseArray[1] = chidArray2;
 //Three Dimensional Array
 var grandparents = [];
 var parents1 = [];
 var parents2 = [];
 var child1 = ["john", "ram"];
 var child2 = ["mona", "steve", "mark"];
 var child3 = [];
 var child4 = ["chris"];
 parents1[0] = child1;
 parents1[1] = child2;
 parents2[0] = child3;
 parents2[1] = child4;
 grandparents[0] = parents1;
 grandparents[1] = parents2;
 console.log("printing Steve");


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


Published by

Dhananjay Kumar

Dhananjay Kumar is Developer Evangelist for Infragistics. He is a 8 times Microsoft MVP and well respected Developer Advocate in India.He is the author of 900+ Blog Posts, and can often be found speaking around India at conferences and hosting free workshops for programmers across the country. So far, he has hosted 60 free workshops on various topics like JavaScript, Angular, WCF, ASP.NET MVC, C#, Azure etc. Follow him on twitter @debug_mode for all the updates about his blog posts and workshops. You can send him email at debugmode [at] outlook [dot] com

3 thoughts on “Working with Array of Arrays in JavaScript”

Leave a Reply

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

You are commenting using your 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