Can someone help me understand the strange behavior I'm experiencing with Array.prototype.fill
? Here's an example:
const arr = new Array(5)
.fill([]);
arr[0].push('element pushed to 0 only');
console.log(arr[1]); // ['element pushed to 0 only']
It seems like all elements in the array are connected and changing one affects the others. I believe all references point to the same array, but I don't know why this is happening. Any insights on this?