var a = new Array(4);
This defines a new array object with an explicit length of 4, but no elements.
var b = [undefined, undefined, undefined, undefined];
This defines a new array object with an implicit length of 4, with 4 elements, each with the value undefined
.
From the docs:
callback is invoked only for indexes of the array which have assigned
values; it is not invoked for indexes which have been deleted or which
have never been assigned values.
For array a
, there are no elements that have been assigned values, so it does nothing.
For array b
, there are four elements that have been assigned values (yes, undefined
is a value), so it maps all four elements to the number 14
.