Double execution with the last value


#1


https://www.codecademy.com/courses/javascript-beginner-en-ASGIv/2/1?curriculum_id=506324b3a7dffd00020bf661#


I wrote two loops and expected that they would give the same result. But the while-loop gave: "0", "1", "2", "3", "4" and one more "4". Why!?
How did the secont "4" appeared?


for(var i=0; i<5; i++) {console.log(i);}
var j=0;
while(j<5) {console.log(j);
j++;}


#2

its console behavior, you update a variable after the last console.log, causing this variable to be logged


#3

Thank you! But why "4"? The last update is "5", isn't it?


#4

no, because you use post-increment, if you use pre-increment, then it would be 5:


#5

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