# Iteration with JavaScript For Loops

Quoting the example:
“var ourArray = [];
for (var i = 0; i < 5; i++) {
ourArray.push(i);
}
ourArray will now contain [0,1,2,3,4].”

I don’t understand why there is a 0 in the final array. If the variable i start from 0, we check the condition, it’s true, and it does i++ which means i = i + 1, which is 0 = 0 + 1 which is 1 no? So at this point, i should be already = 1, so when execute the code ourArray.push(i); it should be pushing as the first element of the array 1. Thank you.

The final part of a `for` loop (in this case `i++`) happens at the end of each iteration of the loop.

That is a good question.

The increment part does not happen until after the loop cycles. i =0 until the end of the first cycle and then becomes 1. If it did not work that way you could never use i = 0 for calculations, etc.

There’s more here

1 Like

Just solved an issue I had with this. The exercise asks for an array of 1-5 but I had initialized i as 0 meaning it was actually creating an array with 0-5. If I initialize i as 1 and go up to 6 (i.e. while i < 6) it works.