JavaScript’s index is 32 bit, so it seems that the array index should be able to go up to 4294967295 for a total of 4294967296 elements. But in fact the highest index is 4294967294. Since an array has a length property, I don’t see a reason for having a null as the last element. Is there a reason the maximum index is 4294967294 but not 4294967295?
JavaScript’s index is 32 bit, so it seems that the array index should be
Share
This is because when you create an array using the
Arrayconstructor you may supply it an optionallengthas follows:The
lengthof an array is a 32-bit unsigned integer. Hence the length of the array may range from0toMath.pow(2, 32) - 1which is4294967295.For an array of length
nthe indices range from0ton - 1. Hence the maximum index of a JavaScript array is(Math.pow(2, 32) - 1) - 1orMath.pow(2, 32) - 2, which is4294967294.Thus a JavaScript array may hold a maximum of
4294967295elements and not4294967296elements.I know. It’s pretty illogical, but then again one element won’t make a lot of difference.