1

I am learning JavaScript and recently met a problem about array of JavaScript.

The numeric index of JavaScript's array is described as 32 bit, I think its max index is 2 to the power of 32 minus 1,But the right is 2 the power of 32 minus 2. I don' know why? Can anyone tell me?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 2
    How did you determine the maximum allowed index of an array? – glhrmv Jun 29 '17 at 15:06
  • @dogui i have try to use 4294967295 as numeric index of array,but i found that it will be dealt as string index when i output it by the method of tostring.So i determine its maximum index is 4294967294 but not 4294967295 – Winson Song Jun 29 '17 at 16:35

2 Answers2

2

Posting this to show you are correct about the max INDEX being 4294967294 since it is the max LENGTH -1 and the max LENGTH is 232-1

So from 0 to 4294967294 you have 4294967295 elements

All JS arrays start at 0

new Array(4294967295) is the largest array you can define because you need to pass an unsigned int to the constructor Hence the largest index you can have is one less than that

MDN Array

arrayLength

If the only argument passed to the Array constructor is an integer between 0 and 232-1 (inclusive), this returns a new JavaScript array with its length property set to that number (Note: this implies an array of arrayLength empty slots, not slots with actual undefined values). If the argument is any other number, a RangeError exception is thrown.


Wikipedia
The number 4,294,967,295, equivalent to the hexadecimal value FFFFFFFF, is the maximum value for a 32-bit unsigned integer in computing. It is therefore the maximum value for a variable declared as an unsigned integer

var arr = new Array(4294967294);
arr[arr.length-1]="one but last";
console.log("Length: "+arr.length,"Index = "+(arr.length-1)+": "+arr[arr.length-1]); 
arr.push("Last");
console.log("Length: "+arr.length,"Index = "+(arr.length-1)+": "+arr[arr.length-1]); 
arr.push("One too far");
console.log("Length: "+arr.length,"Index = "+(arr.length-1)+": "+arr[arr.length-1]); 
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • @4castle I updated. You are absolutely correct – mplungjan Jun 29 '17 at 15:20
  • I know what you mean.But i don't know why array only have 4294967295 elements,32-bit unsigned integer have 4294967296 elements,from 0 to 4294967295,why javascript's array can't use 4294967295 as numeric index.I don't realize it. – Winson Song Jun 29 '17 at 16:29
  • Because all JS arrays start at 0! – mplungjan Jun 29 '17 at 16:38
  • i know array start at 0,but why can't use 4294967295 as index?if array use index from 0 to 4294967294,it just has 4294967295 elements.But 32-bit unsigned integer have 4294967296 elements, from 0 to 4294967295.Javascript's array can use 0 as index, why can't use 4294967295 index?Is the 4294967295 being used to another way? – Winson Song Jun 29 '17 at 16:56
  • See update. It is quite simple. – mplungjan Jun 29 '17 at 17:05
  • Wow...i see.I understand finally.Thank you very much.It puzzle me daylong. – Winson Song Jun 29 '17 at 17:26
  • You are welcome. – mplungjan Jun 29 '17 at 17:27
0

When you create an array using the Array constructor you may supply it an optional length as follows:

The length of an array is a 32-bit unsigned integer. the length of the array may range from 0 to Math.pow(2, 32) - 1 which is 4294967295.

MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
  • 1
    Stating explicitly that the OP is wrong would make a much better answer than just quoting a manual (which is what this reads like). – deceze Jun 29 '17 at 15:06
  • @deceze of course, while making it a much clearer answer it would also make it a wrong answer. The OP is right. – Bergi Jun 29 '17 at 15:11