Accessing Array beyond its size in javascript

arrays, javascript

Solution

An array doesn't have to hold all the items from `0` to `N` to contain one with index `N`.

That's because arrays in JavaScript engines can switch to a dictionnary mode when the holes are too big, those arrays are called sparse arrays (vs dense arrays).

It's important to know this distinction because the implementation is leaking on one point : performance. You should read this on this topic : http://www.html5rocks.com/en/tutorials/speed/v8/

But regarding indexes starting at 2³², sebcap26 is right, there's a distinction due to the fact the index is handled as a string. This distinction is important and can be verified by logging `a.length` : you'll see the length isn't impacted by such an element. There's no exception or error per se but it makes it impossible to use normal array operations like iterating up to the length or using array functions like `map` or `filter` (the elements with index greater than the numeric index limit are ignored by those functions).

Problem

I read in a particular book that an array in JavaScript can hold 4,294,967,295 items and would throw exception if the number reaches beyond that. I tried out the functionality using the following code: ``` var a = ["a","b","c"]; a[4294967300] = "d"; console.log(a[4294967300]); ``` It shows the output "d" and no exception or error. Am I missing something here? Can someone put some light on the topic and share some knowledge regarding max array items in JavaScript and various scenarios related to it?

Original source

Related problems