To my knowledge, if we create multiple variables using Array().fill()
, it will create unique variables without any reference. Below are two code blocks and output.
let [a, b] = Array(2).fill(false);
console.log(a, b); // Output: false false
a = true;
console.log(a, b); // Output: true false
let [x, y] = Array(2).fill([]);
console.log(x, y); // Output:[] []
x.push(1);
console.log(x, y); // Output:[1] [1]
The first code block is okay, no reference is there. But what happens in the second one. I'm expecting the output "[1] []". Please clarify once what is happening here.