JavaScript seems to calculate the array length property by the number of the last index in the array rather than counting the number of items in the array. Example:
var testArray = ['a', 'b', 'c'];
console.log(testArray.length); //3
testArray[8] = 'i';
console.log(testArray.length); //9
Normally I would use push() and wouldn’t add a value to an array using bracket notation, but this was a bit of a surprise. Is there any reason for this implementation of assigning a value to Array.length? The only thing I can seem to come up with is that it just uses the last index as a performance optimization to avoid counting the array items.
1
The length of an array is the number of elements it contains whereby it does not matter if those elements are valid objects or values or just “undefined”. If you add an element at position 8 you automatically generate 5 additional elements between your first three elements and the new one as you can see if you run
for(var i = 0; i < testArray.length; i++){console.log(testArray[i]);}
(Those may or may not take real space in memory, depending on the actual implementation)
Being “equaly spaced” is somewhat the definition of an array (compared to other container types like lists or dictionaries).
3
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.
3