The length of the Array is equal to the number of entries assuming they were all full. Sparse arrays do not have a length of the number of elements in them, but based on the maximum index in them
For example:
let a = [];
a[10] = 1;
console.log(a.length);
console.log(a);
The length is 11 (0 to 10) and you can see that there are values of undefined for index 0 to 9.
Even setting the last value to undefined does not affect the length since there is still a value of undefined in that position.
let a = [];
a[9] = 1;
a[10] = 1;
console.log(a.length);
a[10] = undefined
console.log(a.length);
Using delete still does not affect the length.
let a = [];
a[9] = 1;
a[10] = 1;
console.log(a.length);
delete a[10];
console.log(a.length);
The only way to change the length is to create a new array as a subset of the original:
let a = [];
a[9] = 1;
a[10] = 1;
console.log(a.length);
console.log(a);
a = a.slice(0,9);
console.log(a.length);
console.log(a);
ion every loop. Likei--instead ofi++array[i]only works if you count an undefined values as working.iwill keep incrementing and eventually wrap around to negative 2's complement values and terminate the loop since an integer has a finite maximum value. Your loop is going to go around a lot for no reason taking a much longer time than needed. What exactly does "do something" do?