How to get matrix size in JavaScript

1 Answer

0 votes
const matrix = [
  [2, 0, 5, 9],
  [0, 4, 0, 3],
  [0, 8, 0, 1]
];

// Get the number of rows
const rows = matrix.length;

// Get the number of columns
const columns = matrix[0].length;

const totalCells = rows * columns;

console.log(`Matrix size: ${rows} rows x ${columns} columns`);
console.log(`Total Cells: ${totalCells}`);


/*
run:

Matrix size: 3 rows x 4 columns
Total Cells: 12

*/

 



answered Oct 2, 2025 by avibootz
...