1

If I have an array = [8,7,6,5,4] that I want to loop through, why does the following for loop still work yet the length of the array is 5 and there is no element at index 5 of the array?

for(let i=array.length;i>=0;i++){
  //do something 
}

I know that it would be more accurate to subtract 1 from the length, but why does the code above still work

6
  • You have to deduct 1 from i on every loop. Like i-- instead of i++ Commented Apr 4, 2019 at 15:46
  • array.forEach(item => /* ...Do something...*/ ) Commented Apr 4, 2019 at 15:46
  • 2
    It depends on what you mean by "work". You're not doing anything in the loop, so we don't know what works or doesn't. Accessing array[i] only works if you count an undefined values as working. Commented Apr 4, 2019 at 15:47
  • Can you show us a snippet with an example of that code "working"? Commented Apr 4, 2019 at 15:48
  • 1
    I know it would be more accurate... NO, it would be correct (versus incorrect). It's not about more or less "accurate". i will 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? Commented Apr 4, 2019 at 15:48

5 Answers 5

3

Almost. You have to:

  • Decrease i instead of increasing it, and
  • Start from array.length-1, because array indexes start from 0, not 1.

So use instead:

for (let i = array.length-1; i >=0 ; i--) {
Sign up to request clarification or add additional context in comments.

Comments

0

It occurs cause arrays in Javascirpt are not limited to the size that they were declared. So, it implies that if you try to access an empty position you will receive undefined. For example, in this case, if you do array[5] you will receive undefined and not a ArrayIndexOutOfBoundsException as it would happen in Java.

Also, you should take a look in your code. Since you're iterating the array backwards, you sohuld decrement i with i-- instead of incrementing it.

1 Comment

Thanks, this answers my question best, the decrement of i was a typo.
0

Because its like this:

const array = [1,2,3,4];
for(let i=10;i>=0;i--){
  console.log(array[i]);
}

You can for cycle what ever you want. In javascript everything is an object, including array. Accessing something that does not exist in object with [] returns undefined.

Comments

0
for(let i=array.length-1;i>=0;i--){
  //do something 
}

you need to set i to the length of the array -1 (because array index starts at 0). Then each time you go through the loop you need to subtract one from i (the i--)

you code works but will run through the loop a huge amount of times until the number overflows into a negative. Try adding a console.log(i) to your loop to see what i mean. (it will likely freeze chrome if you do it in the inspector)

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.