(new Array(10)).map(function() { return 1;}) returns [, , , , , …] … Why? [duplicate]

So. I would expect the following code to return [1,1,1,1…].

(new Array(10)).map(function() { return 1;})
But it returns [, , , , , …].

Right, because new Array(10) creates an array with no elements with a length of 10, and map only iterates over elements that actually exist. (And yes, this is surprising. 🙂 )

Moreover, (new Array(10)).length == 10 and (new Array(10))[0] == undefined are true.

Again correct, because (again) new Array(10) doesn’t put any elements in the array, so accessing [0] gives you undefined.

JavaScript’s standard arrays aren’t really arrays at all, and they can have a length that’s a positive number without having any entries in them. They’re a form of “sparse” array.

Let’s take a simpler example:

var a = new Array(10);
a[2] = 1;

That array contains one element, the element at index 2. There is no element at index 0, no element at index 1, and no elements at indexes 3 and above. It just has gaps there. You can tell by asking it:

console.log(0 in a); // "false"
console.log(1 in a); // "false"
console.log(2 in a); // "true"

Standard arrays in JavaScript are just objects with special behavior assigned to length, special behavior assigned to a class of property names (loosely, numeric ones), and that are backed by Array.prototype.

This is all in contrast to the newer “typed” arrays, Int32Array and such, which are true arrays in the traditional sense.

Leave a Comment