From MDN:
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).
When you declare an array using new Array()
, all of the elements are undefined, but they have not been assigned undefined
as a value. Therefore, they are skipped in the call to map()
.
You can use join()
and split()
to explicitly assign undefined
to each element, and you'll then get the expected output:
(new Array(20).join(undefined).split(undefined)).map(function() { console.log(arguments); })