Different between a++ / a=a+1 a+=1?


#1


In this while loop,If I use a=a+1, the result is 1 2 3 4 5.but If I change it to a++,the result change to 1 2 3 4 4.Can anyone explain that?


var a =1;
while(a<5){
    console.log(a);
    a++;}

1
2
3
4
4

var a =1;
while(a<5){
    console.log(a);
    a=a+1;}

1
2
3
4
5


#2

this is console behaviour combined with update order. Not much to worry about.

the console just echoes the last value, you can always put a++ before the console.log to get the correct output


#3

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