The question is, what is the difference between Array(5) and [...Array(5)] See below variable c and d is the same yet .map treats it differently.
let a = [...Array(5)].map((k,i)=>{
console.log("Hello");
return i;
})
console.log(a);
let b = Array(5).map((k,i)=>{
console.log("Hello");
return i;
})
console.log(b);
let c = Array(5)
let d = [...Array(5)]
console.log(c) // c and d seem to be the same
console.log(d)
Array(5)Array(5)returns an array with an initial capacity of 5 elements, all of which are empty.[...Array(5)]returns an array with 5 undefined elements.mapskips over empty elements.undefined, this snippet works just fine:[undefined].map(() => {console.log('ayo')}). It specifically does not run in this case because it's empty slots, which is different fromundefined. See my answer below. @HereticMonkey