JavaScript does not provide the multi-dimensional array natively or provide any method to define the 2D array. However, you can create a multi-dimensional array by defining the array of items, where each item is also another array.

JavaScript 2D Array

To create the 2D array in JavaScript, we have to create an array of array. To declare a 2D array, you use the same syntax as declaring a one-dimensional array.

Syntax of 1D Array

let data = [];

To declare the 2D array, use the following syntax.

Syntax of 2D Array

let data = [
  [],
  [],
  []
];

In the above code, we have defined an empty 2D array.

Let’s define a 2D array with some values.

// app.js

let data = [
  ['Millie', 15],
  ['Finn', 17],
  ['Dustin', 17]
];

console.log(data);

Output

[ [ 'Millie', 15 ], [ 'Finn', 17 ], [ 'Dustin', 17 ] ]

In the above data array, the first dimension represents the name of the actor, and the second one shows the age of each actor.

#javascript #array #developer

JavaScript 2D Array: Create Two Dimensional Array in JavaScript
5.45 GEEK