What Is a Two-Dimensional Array?
A two-dimensional array, commonly called a 2D array, is an array whose elements are arranged in rows and columns. Each element is identified using two indexes: one for the row and one for the column.
Two-dimensional arrays are commonly used to represent tables, matrices, grids, game boards, spreadsheets, and other data where values naturally have two coordinates.
Two-Dimensional Array Example
Consider the following 2D array:
[
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
]
This array contains three rows and three columns. It can also be viewed as a matrix:
Column
0 1 2
Row 0 10 20 30
Row 1 40 50 60
Row 2 70 80 90
The value 50 is located at row index 1 and
column index 1.
Rows and Columns
A 2D array consists of individual one-dimensional arrays, where each inner array generally represents a row.
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
Here:
- There are 3 rows.
- Each row contains 3 elements.
- There are 3 columns in this rectangular matrix.
- The first index identifies the row.
- The second index identifies the column.
Accessing Elements in a 2D Array
An element in a two-dimensional JavaScript array can be accessed using two bracket operations.
const matrix = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
];
console.log(matrix[1][2]);
// 60
The expression matrix[1][2] means:
[1]selects the second row.[2]selects the third element from that row.
2D Array Indexing
JavaScript arrays use zero-based indexing. Therefore, both row and column indexes start at zero.
[
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
]
matrix[0][0] → 10
matrix[0][2] → 30
matrix[1][1] → 50
matrix[2][0] → 70
matrix[2][2] → 90
Creating a Two-Dimensional Array
A simple way to create a 2D array in JavaScript is to define an array containing other arrays.
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
Each inner array represents one row. This representation is often called a nested array.
Creating a 2D Array With a Given Size
A 2D array can also be created dynamically when the number of rows and columns is known at runtime.
const rows = 3;
const columns = 4;
const matrix = Array.from(
{ length: rows },
() => Array(columns).fill(0)
);
console.log(matrix);
This creates three independent rows, each containing four zero values.
Traversing a Two-Dimensional Array
Traversal means visiting each element of the array. Because a 2D array contains rows and columns, nested loops are commonly used to visit every element.
const matrix = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
];
for (let row = 0; row < matrix.length; row++) {
for (let column = 0; column < matrix[row].length; column++) {
console.log(matrix[row][column]);
}
}
The outer loop moves through the rows, while the inner loop moves through the elements within the current row.
Row-Wise Traversal
Row-wise traversal processes all elements of one row before moving to the next row.
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
for (const row of matrix) {
for (const value of row) {
console.log(value);
}
}
The values are visited in this order:
1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9
Column-Wise Traversal
Column-wise traversal processes one column at a time. For a rectangular matrix, the outer loop represents the column and the inner loop represents the row.
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
const columns = matrix[0].length;
for (let column = 0; column < columns; column++) {
for (let row = 0; row < matrix.length; row++) {
console.log(matrix[row][column]);
}
}
The traversal order is:
1 → 4 → 7 → 2 → 5 → 8 → 3 → 6 → 9
Updating an Element in a 2D Array
An individual element can be updated by assigning a new value to its row and column position.
const matrix = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
];
matrix[1][1] = 500;
console.log(matrix);
// [
// [10, 20, 30],
// [40, 500, 60],
// [70, 80, 90]
// ]
When the row and column indexes are already known, accessing and updating an element is typically O(1).
Adding a Row
Because a JavaScript 2D array is an array containing other arrays, a new
row can be added using push().
const matrix = [
[1, 2],
[3, 4]
];
matrix.push([5, 6]);
console.log(matrix);
// [
// [1, 2],
// [3, 4],
// [5, 6]
// ]
Adding an Element to a Row
A value can be added to an existing row by accessing that row and calling
push().
const matrix = [
[1, 2, 3],
[4, 5, 6]
];
matrix[0].push(10);
console.log(matrix);
// [
// [1, 2, 3, 10],
// [4, 5, 6]
// ]
JavaScript arrays do not require every row to have the same length, so this operation creates a non-rectangular, or jagged, structure.
Deleting a Row
A row can be removed using splice() or other array methods.
const matrix = [
[1, 2],
[3, 4],
[5, 6]
];
matrix.splice(1, 1);
console.log(matrix);
// [
// [1, 2],
// [5, 6]
// ]
Deleting an Element From a Row
To remove an individual element, access its row and use an array deletion method on that inner array.
const matrix = [
[10, 20, 30],
[40, 50, 60]
];
matrix[0].splice(1, 1);
console.log(matrix);
// [
// [10, 30],
// [40, 50, 60]
// ]
Finding the Number of Rows and Columns
The number of rows is available through the outer array's
length. For a rectangular 2D array, the number of columns
can be obtained from the first row.
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
const rows = matrix.length;
const columns = matrix[0].length;
console.log(rows);
// 3
console.log(columns);
// 3
If the array can contain empty rows or rows with different lengths, the code should account for those cases instead of assuming that every row has the same number of columns.
Rectangular vs Jagged 2D Arrays
A rectangular 2D array has the same number of elements in every row. A jagged array contains rows with different lengths.
Rectangular Array
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Jagged Array
[
[1, 2],
[3, 4, 5],
[6]
]
JavaScript supports both structures because each row is itself an independent array.
Searching in a Two-Dimensional Array
Searching for a value across a 2D array generally requires examining the rows and the elements within each row.
function searchMatrix(matrix, target) {
for (let row = 0; row < matrix.length; row++) {
for (let column = 0; column < matrix[row].length; column++) {
if (matrix[row][column] === target) {
return [row, column];
}
}
}
return [-1, -1];
}
const matrix = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
];
console.log(searchMatrix(matrix, 60));
// [1, 2]
The returned pair identifies the row and column where the target was found.
Calculating the Sum of All Elements
Nested traversal can be used to perform calculations across every value in a matrix.
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
let sum = 0;
for (const row of matrix) {
for (const value of row) {
sum += value;
}
}
console.log(sum);
// 45
Finding the Largest Element
The same nested traversal pattern can be used to find the largest value in a 2D array.
const matrix = [
[12, 5, 19],
[7, 25, 14],
[9, 3, 18]
];
let largest = matrix[0][0];
for (const row of matrix) {
for (const value of row) {
if (value > largest) {
largest = value;
}
}
}
console.log(largest);
// 25
Transposing a Matrix
The transpose of a matrix converts rows into columns and columns into rows.
Original:
[
[1, 2, 3],
[4, 5, 6]
]
Transpose:
[
[1, 4],
[2, 5],
[3, 6]
]
A straightforward JavaScript implementation is:
function transpose(matrix) {
const rows = matrix.length;
const columns = matrix[0].length;
const result = [];
for (let column = 0; column < columns; column++) {
const newRow = [];
for (let row = 0; row < rows; row++) {
newRow.push(matrix[row][column]);
}
result.push(newRow);
}
return result;
}
const matrix = [
[1, 2, 3],
[4, 5, 6]
];
console.log(transpose(matrix));
Time Complexity of 2D Array Traversal
If a matrix contains r rows and c columns, a
complete traversal visits every element once.
Rows = r
Columns = c
Total elements = r × c
Traversal time = O(r × c)
For a square matrix containing n rows and n
columns, this becomes O(n²).
Space Complexity of a 2D Array
A 2D array containing r × c elements requires space
proportional to the number of stored elements.
Space complexity = O(r × c)
For a square matrix of size n × n, the space requirement is
O(n²).
Common Applications of Two-Dimensional Arrays
Two-dimensional arrays are useful whenever information naturally has two dimensions.
- Matrix calculations.
- Chess and board-game layouts.
- Grid-based games.
- Image and pixel representations.
- Tables and tabular data.
- Seating arrangements.
- Maps and grid-based navigation.
- Dynamic programming tables.
- Graph adjacency matrices.
Two-Dimensional Arrays in Grid Problems
Many algorithmic problems represent a grid using a 2D array. Each position can be identified by a row and column coordinate.
const grid = [
[".", ".", "#", "."],
[".", "#", ".", "."],
[".", ".", ".", "#"],
["#", ".", ".", "."]
];
Algorithms can then inspect neighboring positions by changing the row and column indexes. This pattern appears frequently in grid traversal, pathfinding, flood-fill, and matrix-based problems.
Common Mistakes With 2D Arrays
- Mixing up row and column indexes.
- Using the number of rows as the number of columns without checking the row length.
- Using only one loop when every element must be visited.
- Accessing a row or column that does not exist.
- Assuming every row has the same length when working with jagged arrays.
- Creating multiple rows that accidentally share the same array reference.
Avoiding Shared Row References
When creating a 2D array dynamically in JavaScript, each row should be a separate array. Reusing the same inner array can cause changes to one row to unexpectedly affect every row.
A safe approach is to create a new row for every iteration:
const matrix = Array.from(
{ length: 3 },
() => Array(3).fill(0)
);
matrix[0][0] = 10;
console.log(matrix);
// [
// [10, 0, 0],
// [0, 0, 0],
// [0, 0, 0]
// ]
Two-Dimensional Arrays vs One-Dimensional Arrays
A one-dimensional array stores elements along a single sequence, while a two-dimensional array organizes elements using rows and columns.
| Feature | 1D Array | 2D Array |
|---|---|---|
| Dimensions | One | Two |
| Indexing | One index | Row and column indexes |
| Typical representation | [1, 2, 3] | [[1, 2], [3, 4]] |
| Common use | Lists and sequences | Matrices and grids |
Key Takeaways
- A two-dimensional array organizes data into rows and columns.
- Each element is accessed using a row index and a column index.
- JavaScript commonly represents 2D arrays as arrays containing inner arrays.
- Nested loops are commonly used to traverse every element.
- Row-wise and column-wise traversal are common processing patterns.
- A rectangular 2D array has equal-length rows, while a jagged array can have different row lengths.
- Known-position access is typically O(1), while complete traversal takes O(r × c).
- Two-dimensional arrays are widely used for matrices, grids, tables, and dynamic programming problems.
- When creating a 2D array dynamically, each row should have its own array reference.