Length property of Array is not actually the items count
Zavtramen opened this issue · 3 comments
Actually, the length property can cause problems when you are using negative indexes.
It's relates more to the maximal used index, than to items count.
For example:
var a = [];
a[-10] = 1;
console.log(a.length) //0
I think it's well known fact and it should be mentioned.
Arrays in javascript are stored as a hash map, so you can do things like:
var a = [];
a[-1] = 'foo';
a['banana'] = 'bar';
However these values are not part of the array in any real sense. For example the .map method does not iterate through them, no other array methods interact with negative indexes, and console.log will not print negative indexes.
Conceptually arrays are indexed from zero up (at least in most conventional languages).
Setting an index greater than the range of the array causes javascript to extend the array length, filling intermediate values with undefined.
But people should know about this feature, otherwise they can get stucked then using negative indexes (which in some other programming languages can be used this way)
BTW, for ... in loop counts this items, of course due to Object nature of the Array, but still.
I think at least this should be mentioned.
Thanks, we'll consider it.