Understanding Multidimensional Arrays for Coding with JavaScript

By Chris Minnick, Eva Holland

Not only can you store arrays inside of arrays with JavaScript, you can even put arrays inside of arrays inside of arrays. This can go on and on. An array that contains an array is called a multidimensional array. To write a multidimensional array, you simply add more sets of square brackets to a variable name. For example:

var listOfLists[0][0];

Multidimensional arrays can be difficult to visualize when you first start working with them.

A pictorial representation of a multidimensional array.
A pictorial representation of a multidimensional array.

You can also visualize multidimensional arrays as hierarchal lists or outlines. For example:

Top Albums by Genre

  1. Country

    Johnny Cash:Live at Folsom Prison

    Patsy Cline:Sentimentally Yours

    Hank Williams:I’m Blue Inside

  2. Rock



    Lou Reed:Transformer

  3. Punk


    The Dead Milkmen:Big Lizard in My Backyard

    Patti Smith:Easter

Here is a code that would create an array based on what you see above:

var bestAlbumsByGenre = []
bestAlbumsByGenre[0] = “Country”;
bestAlbumsByGenre[0][0] = “Johnny Cash:Live at Folsom Prison”
bestAlbumsByGenre[0][1] = “Patsy Cline:Sentimentally Yours”;
bestAlbumsByGenre[0][2] = “Hank Williams:I’m Blue Inside”;
bestAlbumsByGenre[1] = “Rock”;
bestAlbumsByGenre[1][0] = “T-Rex:Slider”;
bestAlbumsByGenre[1][1] = “Nirvana:Nevermind”;
bestAlbumsByGenre[1][2] = “Lou Reed:Tranformer”;
bestAlbumsByGenre[2] = “Punk”;
bestAlbumsByGenre[2][0] = “Flipper:Generic”;
bestAlbumsByGenre[2][1] = “The Dead Milkmen:Big Lizard in my Backyard”;
bestAlbumsByGenre[2][2] = “Patti Smith:Easter”;