Two Dimensional Arrays

JavaScript doesn't support two dimensional arrays. However, you can emulate a two dimensional array. Here's how.

Visualizing Two Dimensional Arrays

Whereas, one dimensional arrays can be visualized as a stack of elements, two dimensional arrays can be visualized as a multicolumn table or grid.

For example, we could create a two dimensional array that holds three columns of data; a question, an answer, and a topic.

Two Dimensional Array
0 Arrays What is an array? An ordered stack of data
1 Arrays How to create an array? Assign variable name to array object, then assign values to the array.
2 Arrays What are two dimensional arrays? An ordered grid of data

Creating Two Dimensional Arrays

Generally, creating two dimensional arrays is very similar to creating one dimensional arrays. Some languages allow you to create two dimensional arrays simply by adding an index item, however JavaScript doesn't support two dimensional arrays.

JavaScript, does however, allow you to emulate a two dimensional array. You can do this by creating an "array of an array".

To do this, you create an array, loop through the array, and for each element, you create another array. Then, you simply add an index for each column of your grid. In JavaSript this would look something like this:

Result:

0 Arrays What is an array? An ordered stack of data
1 Arrays How to create an array? Assign variable name to array object, then assign values to the array.
2 Arrays What are two dimensional arrays? An ordered grid of data