We all know the difference between array literals and array constructors is subtle but can be important. There seems to be another difference that is not noted in the referenced link. Take the following:
var x = new Array(5); // [undefined x 5];
var newArr = x.map(function () {return 'newValue'});
console.log(newArr); // [undefined x 5];
vs
var y = [undefined, undefined, undefined, undefined, undefined];
var newArr = y.map(function () {return 'newValue'});
console.log(newArr); // ['newValue', 'newValue', 'newValue', 'newValue', 'newValue'];
I would expect x
and y
to both be array instances and return the same result from the .map
method. Seemingly oddly though, the array x
produces an array that is not mappable whilst the literal y
is mappable.
Why does x
and y
return different results from the .map
method?
Thanks for any help.