javascript - difference between a++ and ++a in while loop -
this question has answer here:
i trying figure out, why end different outcome, when use a++ versus ++a in while loop? know postfix , prefix operations still not why getting results get
var = 1; while (a < 3) { console.log(a); a++; }
i ger results: 1, 2, 2 , when using ++a instead of a++ different outcome.
var = 1; while (a < 3) { console.log(a); ++a; } in case 1,2,3. can explain operations order step step , why output get?
you'll 1,2,2 or 1,2,3 if run in console - outside console both output 1,2 - check console when running fiddle
when running in console, last number see in console "result" of last operation ... so, 2 (because of post increment) in first case, , 3 (because of pre increment) in second -
if add 1 line after } a; - both show 1,2,3 - so
var = 1; while (a < 3) { console.log(a); a++; } a; and
var = 1; while (a < 3) { console.log(a); ++a; } a; shows same after while loop finishes
Comments
Post a Comment