+ 2
I didn't get all for loops in Javascript
/* If 'i' has to less than or equal 5. Why the outputs is 6? */ for (i=1; i<=5; i++) {} document.write(i + "<br />");// outputs 6 /* If 'i' has to less than 5. Why the outputs is 5? */ for (i=1; i<5; i++) {} document.write(i + "<br />");// outputs 5 /* I know that document.write is not in the code block but, I still do not understand the because of these outputs. sorry if my English is bad. */ The code: https://code.sololearn.com/W7h5UuUrx5m8/?ref=app
2 Answers
+ 2
In the first part
The for loop code block runs at i = 1, 2, 3....5 and then stops at i = 6 (When i <= 5 is false)
So the next line after the for loop code block captures i at 6.
In the second part,
The for loop code block runs at i = 1, 2,... 4, and stops at i = 5 (when i < 5 is false)
So the next line after the for loop code block captures i at 5.
+ 1
Thanks Gordon I get it the logic now