I\'m trying to set default values on an uninitialized array using the map function but it doesn\'t seem to work, any ideas on how to set default values?
Consider thi
That's how it's described by the ECMAScript Language specification
Here's the relevant part of Array.prototype.map, as described by (§15.4.4.19)
k < len
Pk be ToString(k).kPresent be the result of calling the [[HasProperty]] internal method of O with argument Pk.kPresent is true, then
Since there is no initilized member in your array, calling e.g new Array (1337).hasOwnProperty (42)evaluates to false, hence the condition in step 8.c is not met.
You can however use a little "hack" to do what you want.
Array.apply(null, { length: 5 }).map(Number.call, Number) //[0, 1, 2, 3, 4]
How this works has been thouroughly explained by @Zirak