How to Make and Access a 3d Array in JavaScript

A 3d array is an array made up of arrays of arrays. You can create an array in JavaScript and fill it with more JavaScript arrays to make a 3d array.

An array is an ordered list of values; the values inside it are called the elements of the array. Every array element has a number, an index, you can use to access it.

You can define the elements of an array between square brackets, like so:

const example_array=[1,2,4,8,16];

For the following array examples, I'll be using strings (basically letters and words), though the array elements could be anything.

You can think of the dimension of an array as the number of indexes, or indices, you need to name a value of that array.

Think of dimension in geometry: 1d, 2d, 3d. We have the same thing with arrays.

1D Arrays

A 1d array, or a 1 dimensional array, is the default array you think of when presented with an array.

Here is a simple example, of length 4, expressed in the form of an array literal (where all the elements of the array are shown between square brackets).

const arr=["hey","man","nice","shot"];

2D Arrays

A 2D array is an array of arrays, where each array element contains its own array.

This 2d array is created using the Array() constructor, with several index values being filled with array literals. The array arr has 3 elements, and each element has its own array, of lengths 3, 5 and 2.

let arr=new Array();




You can access its elements by specifying the array index number you need. The example below would access the [1][1] element and print the character 'e' to the console.


3D Arrays

If a 1d array is a regular array, and a 2d array is an array of arrays, a 3d array adds one more layer: it's an array of arrays of arrays.

To create it, you can define each component javascript array element manually, and then combine those component arrays with elements that are also arrays into a new array. (That's a lot of arrays!)

let arr0=new Array();




let arr1=new Array();



let arr=[arr0,arr1];


This code would access the [1][0][2] array element and print the unique string 'team' to the console.