DEV Community

Discussion on: Here's Why Mapping a Constructed Array in JavaScript Doesn't Work

Collapse
 
ann0nip profile image
Juan Martin Gimenez

I think my question is out of the scope of the topic, but:

When we do:


Array(100);

If there are no index keys in the object representation of the array (the object is a vacuum), why we can spread it?? 🤯


const arr = [...Array(100)]

Thank you for this interesting post!

Salute from 🇦🇷!

Collapse
 
sreisner profile image
Shawn Reisner • Edited

Great question. The reason we can spread an array with no index keys is because spread's implementation doesn't require that any index keys exist, unlike map. It blindly loops from 0 to 99, asks the array for array[index], and places whatever's returned in the new array.