Iteration Through an Array with a For Loop

Could someone please tell me why we have to use i++ in order for this code to iterate… thank you

const arr = [10, 9, 8, 7, 6];

for (let i = 0; i < arr.length; i++) {
   console.log(arr[i]);
}

Link to the challenge:

That’s just how the syntax works.

for (/* code to run before the entire loop */; /* condition to check before every loop iteration */; /* code to run after every loop iteration */) {
  // code to run on every loop
}

In this case we want to go through the whole array, so we want i to go up by one after each time we execute the loop body.

I understand now… thank you very much.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.