JavaScript.length

·

1 min read

Did you know, that the Array.length property in JavaScript doesn't actually give you the length of the array but gives a number that is the largest index + 1?

See the code below:
For an Array with 8 items, the length is 101!

var array = [1, 2, 3];

array[4] = 560;
array[-1] = 345;
array.hi = 300;
array[100] = 700;
array[{}] = [];

console.log(array.length);
console.log(Object.keys(array).length);

Output: