I want to create an empty array with a fixed length, and then use .map on it to return a new array. However, it does not work properly.
According to mdn docs :
If the only argument passed to the Array constructor is an integer from 0 to 232-1 (inclusive), this returns a new JavaScript array with the length set for that number.
new Array(3) returns [undefined × 3] . Shouldn't be: [undefined, undefined, undefined] ?
Consider the following examples:
1) Doesn't work.
var a = new Array(3); a.map((x, i) => i);
2) Work.
var a = Array.apply(null, new Array(3)); a.map((x, i) => i);
I tested this on the latest version of Google Chrome Canary.
javascript
Eggy
source share