How to declare, initialize and print 2D array (matrix) in JavaScript

1 Answer

0 votes
const arr = [
  [1, 2, 3, 5],
  [4, 5, 6, 5],
  [7, 8, 9, 5]
];
 
const rows = arr.length;
const cols = arr[0].length;
 
for (let i = 0; i < rows; i++) {
    for (let j = 0; j < cols; j++) {
        console.log(arr[i][j]); 
    }
    console.log(); 
}
 
   
     
     
/*
run:
     
1
2
3
5
 
4
5
6
5
 
7
8
9
5
     
*/

 



answered Feb 21, 2021 by avibootz
edited Sep 14, 2022 by avibootz
...