Q:

why can't map 'undefined' array

As title say, why can't I map a array which contain undefined item?
var foo = new Array(3);
// [ , , ]
var bar = [null, null, null];
// [null, null, null]

foo.map(function(val){return 'test'});
// [ , , ]
bar.map(function(val){return 'test'});
// ['test', 'test', 'test']

maybe the foolish question, but I really want to know the reason.
thanks.

A:

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#Description:

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).

It’s not working because elements haven’t been assigned a value.
If you use
foo[0] = undefined;
foo[1] = undefined;
foo[2] = undefined;

then it’ll work.

