How to get matrix size in TypeScript

1 Answer

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

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

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

const totalCells: number = 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
...