JavaScript Multidimensional Array
Last Updated :
24 Apr, 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 Array
JavaScript 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 Literals
The 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]);
Output[ 'd', 'e', 'f' ]
h
Iterating 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.
JavaScript
let mat = [
[1, 2, 3],
[4, 5, 6]
];
// Add a new row
mat.push([7, 8, 9]);
console.log(mat);
Output[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
- Example 2: Adding an Element at a Specific Position.
JavaScript
let mat = [
[1, 2, 3],
[4, 5, 6]
];
mat[1][1] = 10;
console.log(mat);
Output[ [ 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().
- Example 1: Removing a Row
JavaScript
let mat = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
// Remove the last row
mat.pop();
console.log(mat);
Output[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
- Example 2: Removing an Element at a Specific Position
JavaScript
let mat = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
delete mat[1][1];
console.log(mat);
Output[ [ 1, 2, 3 ], [ 4, <1 empty item>, 6 ], [ 7, 8, 9 ] ]
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.
Best Practices for Working with Multidimensional Arrays:
- Avoid Deep Nesting: Minimize excessive nesting to keep your code maintainable. Consider using objects or maps for complex data structures.
- Use Descriptive Variable Names: Use clear, meaningful names for arrays to make your code more understandable (e.g.,
gameBoard
instead of arr
). - Check Array Lengths: Always check the array’s length before accessing its elements to avoid errors and
undefined
values.
Conclusion
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.
Similar Reads
C++ Multidimensional Array
A multidimensional array is an array with more than one dimension. It means that it can grow in different directions i.e. instead of changing the length only, it can also change in width, depth or more. It is the homogeneous collection of items where each element is accessed using multiple indices.
10 min read
Multidimensional Array in R
Arrays are the R data objects which can store data in more than two dimensions. For example: If we create an array of dimensions (2, 3, 4) then it creates 4 rectangular matrices each with 2 rows and 3 columns. These types of arrays are called Multidimensional Arrays. Arrays can store only data types
3 min read
Perl | Multidimensional Arrays
Multidimensional arrays in Perl are the arrays with more than one dimension. Technically there is no such thing as a multidimensional array in Perl but arrays are used to act as they have more than one dimension. Multi dimensional arrays are represented in the form of rows and columns, also knows as
6 min read
Multidimensional Arrays in Scala
In Scala, multidimensional arrays can be represented as arrays of arrays, where each nested array represents a row or a column. The size of the array can be defined using two integer values representing the number of rows and columns, respectively. Here's an example code with output that demonstrate
4 min read
Multidimensional Arrays in Excel VBA
Multidimensional Arrays are used to store data of similar data types of more than one dimension. A multidimensional array has a dimension up to 60 but usually, we don't use arrays of dimensions more than 3 or 4. Here, we will see how to declare a multidimensional array in many ways and also how to c
2 min read
How to iterate a Multidimensional Array?
Multidimensional arrays are arrays that have more than one dimension. For example, a simple array is a 1-D array, a matrix is a 2-D array, and a cube or cuboid is a 3-D array but how to visualize arrays with more than 3 dimensions, and how to iterate over elements of these arrays? It is simple, just
8 min read
MultiDimensional Data Model
A Multidimensional Data Model is defined as a model that allows data to be organized and viewed in multiple dimensions, such as product, time and location It allows users to ask analytical questions associated with multiple dimensions which help us know market or business trends.OLAP (online analyti
6 min read
Initialization of Multidimensional Arrays in C++
In C++, multidimensional arrays are the type of arrays that have multiple dimensions, i.e., they can expand in multiple directions. In this article, we will discuss how to initialize the multidimensional arrays in C++. Methods to Initialize Multidimensional Array in C++We can initialize multidimensi
3 min read
JavaScript Array from() Method
The Array.from() method is used to create a new array from any iterables like array, objects, and strings. [GFGTABS] JavaScript const a = Array.from("GeeksforGeeks"); console.log(a); [/GFGTABS]Output[ 'G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's' ] Syntax Array.from(objec
2 min read
Types of Arrays in JavaScript
A JavaScript array is a collection of multiple values at different memory blocks but with the same name. The values stored in an array can be accessed by specifying the indexes inside the square brackets starting from 0 and going to the array length - 1([0]...[n-1]). A JavaScript array can be classi
3 min read