Difference between Array.apply(null, Array(x) ) and Array(x)

There is a difference, a quite significant one.

The Array constructor either accepts one single number, giving the lenght of the array, and an array with “empty” indices is created, or more correctly the length is set but the array doesn’t really contain anything

Array(3); // creates [], with a length of 3

When calling the array constructor with a number as the only argument, you create an array that is empty, and that can’t be iterated with the usual Array methods.

Or… the Array constructor accepts several arguments, whereas an array is created where each argument is a value in the array

Array(1,2,3); // creates an array [1,2,3] etc.

When you call this

Array.apply(null, Array(3) )

It get’s a little more interesting.

apply accepts the this value as the first argument, and as it’s not useful here, it’s set to null

The interesting part is the second argument, where an empty array is being passed in.
As apply accepts an array it would be like calling

Array(undefined, undefined, undefined);

and that creates an array with three indices that’s not empty, but have the value actually set to undefined, which is why it can be iterated over.

The main difference is that Array(3) creates an array with three indices that are empty. In fact, they don’t really exist, the array just have a length of 3.

Passing in such an array with empty indices to the Array constructor using apply is the same as doing Array(undefined, undefined, undefined);, which creates an array with three undefined indices, and undefined is in fact a value, so it’s not empty like in the first example.

Array methods like map() can only iterate over actual values, not empty indices.

Leave a Comment