Multi-dimensional Arrays
So far, we've focused on one-dimensional arrays, which are essentially ordered lists of items. But what if you need to represent data that has more than one dimension, like a grid, a table, or coordinates in space? That's where multi-dimensional arrays come in.
What is a Multi-dimensional Array?
Conceptually, a multi-dimensional array is an array of arrays.
- A 2D array (or matrix) is an array where each element is itself another array.
- A 3D array is an array where each element is a 2D array, and so on.
The most common multi-dimensional array you'll encounter is a 2D array, often used to represent things like:
- Grids or Boards: Think of a chessboard, a Tic-Tac-Toe board, or a spreadsheet.
- Images: A simple image can be represented as a 2D array of pixel values.
- Matrices: In mathematics, matrices are fundamental and are naturally represented as 2D arrays.
Representing 2D Arrays in JavaScript
In JavaScript, you create a 2D array by nesting arrays within arrays. Each inner array typically represents a row, and the elements within that inner array represent the columns for that row.
Creating a 2D Array
Here's how you might create a 3x3 matrix (3 rows, 3 columns) in JavaScript:
// A 3x3 matrix
const matrix = [
[1, 2, 3], // Row 0
[4, 5, 6], // Row 1
[7, 8, 9] // Row 2
];
console.log(matrix);
/* Output:
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
*/
Accessing Elements in a 2D Array
To access an element in a 2D array, you use two sets of bracket notation: array[rowIndex][columnIndex].
- The first index
[rowIndex]selects the desired inner array (row). - The second index
[columnIndex]then selects the element within that row.
const gameBoard = [
['X', 'O', 'X'], // Row 0
['O', 'X', 'O'], // Row 1
['X', 'O', 'X'] // Row 2
];
console.log(gameBoard[0][0]); // Accesses element at Row 0, Column 0 -> Output: 'X'
console.log(gameBoard[1][2]); // Accesses element at Row 1, Column 2 -> Output: 'O'
console.log(gameBoard[2][1]); // Accesses element at Row 2, Column 1 -> Output: 'O'
Iterating Through a 2D Array
To process every element in a 2D array, you typically use nested loops. The outer loop iterates through the rows, and the inner loop iterates through the columns of the current row.
const grid = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
];
for (let i = 0; i < grid.length; i++) { // Outer loop for rows
for (let j = 0; j < grid[i].length; j++) { // Inner loop for columns in the current row
console.log(`Element at [${i}][${j}]: ${grid[i][j]}`);
}
}
/* Output:
Element at [0][0]: 10
Element at [0][1]: 20
...
Element at [2][2]: 90
*/
"Jagged" or Irregular Arrays
One interesting aspect of JavaScript's arrays being objects is that inner arrays don't have to be of the same length. This creates what's sometimes called a "jagged array":
const jaggedArray = [
[1, 2],
[3, 4, 5],
[6]
];
console.log(jaggedArray[0].length); // Output: 2
console.log(jaggedArray[1].length); // Output: 3
While flexible, it's generally best practice for 2D arrays representing true matrices or grids to have inner arrays of consistent length to avoid unexpected behavior.
Memory and Performance Considerations
Unlike some languages where a 2D array might be stored as a single contiguous block of memory, in JavaScript, a 2D array is an array whose elements are references to other arrays. These inner arrays might not necessarily be stored right next to each other in memory.
However, for practical purposes, accessing elements array[row][column] still benefits from O(1) constant time complexity, similar to 1D arrays, because once a row is identified (which is an O(1) operation), accessing its column by index is also O(1). Iterating through an M x N matrix will take O(M * N) time, as you need to visit every element.
Multi-dimensional arrays are powerful tools for modeling real-world data that inherently has multiple dimensions. Mastering their creation and manipulation is a key skill in DSA.

