Double execution with the last value


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


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


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


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


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