Filling a matrix with values
In my opinion, the const semantics in JavaScript are too weak to help static code analysis or to allow for compiler optimizations. Where const is (mis-)used to signal immutability, I'd go with e.g. declaring ALL_CAPS identifiers instead.


All const does is assure you that the value assigned to it is the same value throughout the scope. If it was assigned an array, it will be the same array and not suddenly change to another array or a different value altogether.

Mutation of the value, however, is a different story. JavaScript has no concept of immutability.Object.freeze does exist, and primitives are immutable, but that's about it.

For your code, if you're aiming to avoid mutation of your arrays, temporary values, counters and all, swap your loop for array.map and create a range function. Create a range, map through it, fill it with values, repeat.



function range(n){
  return Array(n).fill(0);
}

function matrix(dim, value){
  return range(dim).map(v =&gt; range(dim).map(v =&gt; value));
}

console.log(matrix(10, 2));