Last Updated : 12 Jul, 2025
A multidimensional array in JavaScript is an array that contains other arrays as its elements. These are often used to represent data in a grid or matrix format.
In JavaScript, there is no direct syntax for multidimensional arrays, but you can achieve this by creating arrays within arrays.
Creating a Multidimensional ArrayJavaScript doesn’t have built-in support for multidimensional arrays like some other languages. But we can still create them by making an array where each element is itself another array. This allows you to create 2D or even 3D arrays.
There are two common ways to create a multidimensional array in JavaScript:
1. Using Array LiteralsThe easiest and most common way to create a multidimensional array is by using square brackets ([]), also known as array literals. This method allows you to directly define arrays within arrays.
JavaScript
let matrix = [
[101, 'Ankit', 'Noida'],
[102, 'Ravi', 'Delhi'],
[103, 'Sneha', 'Mumbai']
];
2. Using Array Constructor
Another way to create a multidimensional array is by using the Array() constructor.. This can be useful when you need to dynamically create arrays based on conditions.
JavaScript
let matrix = new Array(3); // Creates a new array with 3 undefined elements
for (let i = 0; i < 3; i++) {
matrix[i] = new Array(3).fill(0); // Each sub-array has 3 elements filled with 0
}
Accessing Elements in a Multidimensional Array
To access an element in a multidimensional array, you can use the index notation array[rowIndex][columnIndex]. Remember that JavaScript arrays are zero-indexed, meaning the first element has an index of 0.
JavaScript
let mat = [
["a", "b", "c"],
["d", "e", "f"],
["g", "h", "i"]
];
console.log(mat[1]);
console.log(mat[2][1]);
[ 'd', 'e', 'f' ] hIterating Over a Multidimensional Array
To iterate over a multidimensional array, you can use nested loops. Each loop will iterate over one dimension of the array.
JavaScript
let mat = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
for (let i = 0; i < mat.length; i++) {
for (let j = 0; j < mat[i].length; j++) {
console.log(mat[i][j]);
}
}
Adding Elements to a Multidimensional Array
To add elements to a multidimensional array, you can use array methods like push(), unshift(), or directly assign values to specific indices.
let mat = [
[1, 2, 3],
[4, 5, 6]
];
// Add a new row
mat.push([7, 8, 9]);
console.log(mat);
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
let mat = [
[1, 2, 3],
[4, 5, 6]
];
mat[1][1] = 10;
console.log(mat);
[ [ 1, 2, 3 ], [ 4, 10, 6 ] ]Removing Elements in a Multidimensional Array
To remove elements from a multidimensional array, you can use methods like pop(), shift(), or splice().
let mat = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
// Remove the last row
mat.pop();
console.log(mat);
[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
let mat = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
delete mat[1][1];
console.log(mat);
[ [ 1, 2, 3 ], [ 4, <1 empty item>, 6 ], [ 7, 8, 9 ] ]
Best Practices for Working with Multidimensional Arrays:JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
gameBoard
instead of arr
).undefined
values.JavaScript does not have built-in support for multidimensional arrays like some other programming languages, but you can simulate them using nested arrays. This approach allows you to work with grids, matrices, or even higher-dimensional data structures. With array methods like push()
, pop()
, shift()
, and splice()
, you can easily modify the elements of multidimensional arrays.
JavaScript Multidimensional Array
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4