When you use Array(4).fill([])
, you are actually filling count with only one array ([]
). All the elements inside count
refer to the same array. This is why when you alter the first element (which is also referenced by the second, third, and fourth element) and print count
, count
's elements all have the same array object. The same referencing scenario in fill happens to all objects, not just arrays.
Here, you can see that the second, third, and fourth element refers (denoted by /**ref:2**/
) to the object with the same id
(/**id:2**/
) as the first element:
let count = new Array(4).fill([])
count[0].push("Test")
console.log(count)