What's the difference between Array(1) and [...Array(1)]? I was sure that both are equivalent until I saw that both behave differently regarding .map:
console.log(Array(1).map(() => 1));
console.log([...Array(1)].map(() => 1));
Why do they behave differently even though both return [undefined]?
console.log(Array(1));
console.log([...Array(1)]);
Array(1)creates an array with 1 empty slot. Spreading it makes an array with one item which isundefined.undefinedare different but when I try to access an empty spot I getundefined?undefinedfor bothobj = {a: 1, c: 3}thenobj.bwill give youundefined. That's what an empty slot is - it doesn't exist. But if you try to read its value, you getundefined. Array iteration methods such as.mapwill skip over empty slots and only act on existing items, even if those items areundefined.