Array.fill is intended to be used to fill all the elements of an array from a start index to an end index with a static value.
This unfortunately means that if you pass in an element such as a new array, your array will actually be filled with many references to that same element. In other words, a
is not filled with six arrays of size six; a
is filled with six pointers to the same array of size six.
You can easily verify this in your developer console:
var a = new Array(6).fill(new Array(6).fill(0));
a
>>> [Array[6], Array[6], Array[6], Array[6], Array[6], Array[6]]
a[0]
>>> [0, 0, 0, 0, 0, 0]
a[1]
>>> [0, 0, 0, 0, 0, 0]
a[0][1] = 1
a[0]
>>> [0, 1, 0, 0, 0, 0]
a[1]
>>> [0, 1, 0, 0, 0, 0]