When you create an array like so:
var arr1 = new Array( 4 );
you get an array that has a length of 4
, but that has no elements. That's why map
doesn't tranform the array - the array has no elements to be transformed.
On the other hand, if you do:
var arr2 = [ undefined, undefined, undefined, undefined ];
you get and array that also has a length of 4
, but that does have 4 elements.
Notice the difference between having no elements, and having elements which values are undefined
. Unfortunately, the property accessor expression will evaluate to the undefined
value in both cases, so:
arr1[0] // undefined
arr2[0] // undefined
However, there is a way to differentiate these two arrays:
'0' in arr1 // false
'0' in arr2 // true