I want to initialize an array of a given size with elements set to undefined
by default.
new Array(5)
returns an array of 5 empty items.
Array.apply(null, Array(5))
returns exactly what I want: a five element array with each element as undefined
.
Why does the second option produce a different array value than the first?
This distinction is important because I can map over an array of empty items but I can for undefined elements.
new Array(5).map(Boolean) // [false, false, false, false, false]
Array.apply(null, Array(5)) // [ <5 empty items>]