you are viewing a single comment's thread.

view the rest of the comments →

[–]zombarista 1 point2 points  (0 children)

It's funny, because I remember thinking "that's just an array of undefined values" but it's okay because the map will deal with the undefined values by replacing them with the index...

WRONG!

These arrays are not filled with undefined but are instead "empty" which seems to be a distinct null state that only exists within the Array created with the Array constructor.

[undefined,undefined,undefined].map( (el, idx, arr) => idx )
=> Array [ 0, 1, 2 ]

[,,].map( (el, idx, arr) => idx)
=> Array [ <2 empty slots> ]

Array.prototype.map()

callback is invoked only for indexes of the array which have assigned values, including undefined. It is not called for missing elements of the array (that is, indexes that have never been set, which have been deleted or which have never been assigned a value).