A multidimensional array is an array of arrays. The most common use is a 2D array (matrix) to represent tabular data.
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
matrix[0][0]; // 1 — row 0, col 0
matrix[1][2]; // 6 — row 1, col 2
matrix[2][1]; // 8 — row 2, col 1
for (let row = 0; row < matrix.length; row++) {
for (let col = 0; col < matrix[row].length; col++) {
console.log(matrix[row][col]);
}
}
Given this 3×3 matrix:
const grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
matrixSum(matrix) that returns the sum of all elements.diagonal(matrix) that returns an array of the main diagonal elements (top-left to bottom-right).#output: "Sum: 45 | Diagonal: 1,5,9".Access elements with two indices: matrix[row][col].