How to check whether a matrix is a square matrix in TypeScript

1 Answer

0 votes
function IsSquareMatrix(matrix: number[][]): boolean {
    const len = matrix.length;

    if (!Array.isArray(matrix) || !matrix.every(row => Array.isArray(row) && row.length === len)) {
        return false;
    }

    return true;
}

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

if (IsSquareMatrix(matrix)) {
    console.log("The matrix is a square matrix.");
} else {
    console.log("The matrix is not a square matrix.");
}




/*
run:

"The matrix is a square matrix." 

*/

 



answered Oct 7, 2025 by avibootz
...