JS: Max number of elements

By Xah Lee. Date: .

Max number of elements

The max number of elements is 2^32 - 1 (which is 4 294 967 295).

When the index is a number between 0 to 2^32-2, inclusive, it is treated as array index, else it's just a property key.

Note: if you create a array with just 1,000,000 items, the browser will become not responsive.

// When array index is beyond 2^32 - 2, it is treated as a property key.

const xx = ["a"];
xx[1] = "b";
xx[2 ** 32] = "c";

console.assert(2 ** 32 === 4294967296);

console.log(xx);
// [ "a", "b", "4294967296": "c" ]

// note the c is printed with a key

console.log(
 Object.getOwnPropertyNames(xx),
);
// [ "0", "1", "length", "4294967296" ]
// all are property keys