To iterate over a Two Dimensional Array in JavaScript, you can use nested loops.
In this example,
matrix
.const matrix = [[0, 1, 2], [1, 2, 3], [2, 3, 4]];
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
console.log(matrix[i][j]);
}
}
0 1 2 1 2 3 2 3 4
In this example,
matrix
.const matrix = [['Hello', 'World'], ['JavaScript', 'TypeScript']];
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
console.log(matrix[i][j]);
}
}
Hello World JavaScript TypeScript
In this tutorial, we learned How to Iterate over a Two Dimensional Array in JavaScript language with well detailed examples.