I rather think it is the other way around: last index is based on the length. I think JS may just be setting the length for you behind the scenes when you assign a value to an (as yet) non-existing index. Which means that you could read your:
testArray[8] = 'h';
as
SetLength(testArray, 9); // or something similar (I'm not too familiar with JS)
testArray[8] = 'h';
There are a few language out there that allow for arrays with other starting indices, but most arrays that allow for dynamic allocation/sizing will have zero-based indices. And that means that there is always a fixed relation between the length of the array and the last index number:
Last index number = Length - 1
Update
JS doesn't do a last index + 1
to get the count. When you do testArray[8] = 'h';
JS doesn't just set the length to 8 + 1
. It first checks how many elements are already in the array. When the array does not yet have enough elements to store something at index 8, it will resize. It resizes the array to just large enough to hold something at index 8, which, because arrays are zero-based, means resizing to 8 + 1.
Length <> number of stored elements
That doesn't mean the array then holds 9 elements. It just means that the array has enough room to do so. If you create an array by just testArray[8] = 'h';
, you are essentially telling JS to allocate enough room for 9 elements and store 'h'
at the element with index 8.