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.
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.
let data = [];
To declare the 2D array, use the following syntax.
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);
[ [ '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