Skip to content

JavaScript multi dimensional arrays

Ramesh Fadatare edited this page Aug 11, 2020 · 1 revision

In JavaScript, we can create multi-dimensional arrays.

JavaScript multi-dimensional arrays

A multi-dimensional array is created by nesting arrays into other arrays.

const nums = [2, 3, 2, [33, 44, 55], [7, 8, [77, 88]]];

console.log(nums[2]);

console.log(nums[3]);
console.log(nums[3][0]);

console.log(nums[4][0]);
console.log(nums[4][2][0]);

We have a multi-dimensional array defined:

const nums = [2, 3, 2, [33, 44, 55], [7, 8, [77, 88]]];

To get an element from the first dimension, we use a single pair of square brackets:

console.log(nums[2]);

This line prints the entire inner array:

console.log(nums[3]);

To get an element from an inner array, we use two pairs of brackets:

console.log(nums[3][0]);

Here we get a value from the third dimension:

console.log(nums[4][2][0]);

This is the output:

2
[ 33, 44, 55 ]
33
7
77

Clone this wiki locally