Algorithm for matrix multiplication in JavaScript

Matrix multiplication is a fundamental operation in linear algebra where two matrices are multiplied to produce a third matrix. In JavaScript, we can implement this algorithm using nested loops to calculate the dot product of rows and columns.

Matrix Multiplication Rules

For matrix multiplication to be valid, the number of columns in the first matrix must equal the number of rows in the second matrix. If matrix A is m×n and matrix B is n×p, the result will be an m×p matrix.

Matrix A (m×n) m rows n cols × Matrix B (n×p) n rows p cols = Result (m×p) m×p Important: n must be equal for multiplication to work

Algorithm Implementation

const multiplyMatrices = (a, b) => {
    if (!Array.isArray(a) || !Array.isArray(b) || !a.length || !b.length) {
        throw new Error('Arguments should be in 2-dimensional array format');
    }
    
    let x = a.length,        // rows in matrix A
        z = a[0].length,     // columns in A / rows in B
        y = b[0].length;     // columns in matrix B
    
    if (b.length !== z) {
        throw new Error('Number of columns in first matrix should equal rows in second matrix');
    }
    
    // Initialize result matrix with zeros
    let product = Array(x).fill().map(() => Array(y).fill(0));
    
    // Triple nested loop for matrix multiplication
    for (let i = 0; i 

Matrix A (3x2):
[ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
Matrix B (2x3):
[ [ 7, 8, 9 ], [ 10, 11, 12 ] ]
Result (3x3):
[ [ 27, 30, 33 ], [ 61, 68, 75 ], [ 95, 106, 117 ] ]

Larger Example

// 5 x 4 matrix
let a = [
    [1, 2, 3, 1],
    [4, 5, 6, 1],
    [7, 8, 9, 1],
    [1, 1, 1, 1],
    [5, 7, 2, 6]
];

// 4 x 6 matrix
let b = [
    [1, 4, 7, 3, 4, 6],
    [2, 5, 8, 7, 3, 2],
    [3, 6, 9, 6, 7, 8],
    [1, 1, 1, 2, 3, 6]
];

// Results in a 5 x 6 matrix
console.log("Large matrix multiplication result:");
console.log(multiplyMatrices(a, b));
Large matrix multiplication result:
[
    [ 15, 33, 51, 37, 34, 40 ],
    [ 33, 78, 123, 85, 76, 88 ],
    [ 51, 123, 195, 133, 118, 136 ],
    [ 7, 16, 25, 18, 17, 22 ],
    [ 31, 73, 115, 88, 73, 96 ]
]

How It Works

The algorithm uses three nested loops:

  • Outer loop (i): Iterates through rows of matrix A
  • Middle loop (j): Iterates through columns of matrix B
  • Inner loop (k): Calculates dot product of row i and column j

Each element in the result matrix is computed as: result[i][j] = ?(A[i][k] × B[k][j])

Time Complexity

The time complexity is O(m × n × p) where m and p are the dimensions of the result matrix, and n is the shared dimension. This makes it suitable for small to medium-sized matrices.

Conclusion

Matrix multiplication in JavaScript requires careful dimension checking and nested loops to compute dot products. The algorithm is straightforward but becomes computationally expensive for large matrices, making it important to validate inputs before processing.

Updated on: 2026-03-15T23:19:00+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements