created by Brian LeRoux & Andrew Lunny. sparodically uncurated by David Trejo.

2013 09 30 Array Constructor2 is Very Undefined

Have you ever thought that Array(3) will return you an array of 3 undefined's, the same as [undefined,undefined,undefined]?

So try this:

    Array(3).forEach(function(elem) { console.log(elem); });

And you will get no result at all, however

    [undefined,undefined,undefined].forEach(function(elem) { console.log(elem); });

will give you 3 nice log entries.

Are the first example's undefined less defined than the second example's undefineds?

Not really. According to forEach spec: "callbackfn is called only for elements of the array which actually exist", and constructor spec says nothing about putting in any elements (even undefined).

@tomalec

Fork me on GitHub