arrayLength If the only argument passed to the Array constructor is an integer between 0 and 232 – 1 (inclusive), this returns a new JavaScript array with its length property set to that number (Note: this implies an array of arrayLength empty slots, not slots with actual undefined values — see sparse arrays).
Array(5) creates a new empty array of a length 5 with all the items as undefined. Calling map() on this empty array has no effect on the callback function (will not be executed).
To solve the issue, you can try Array.from() like following way:
const foo = Array.from({length:5}, () => new Image());
console.log(foo);
2
Answers
From the doc : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Array
More explanation on sparse arrays here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Indexed_collections#sparse_arrays
Array(5)
creates a new empty array of a length 5 with all the items asundefined
. Callingmap()
on this empty array has no effect on the callback function (will not be executed).To solve the issue, you can try
Array.from()
like following way: