+ 2
Difference between x++ or ++x or x+=1 in JavaScript.
Can anyone explain the difference in JavaScript between x++ and ++x and x+=1? I didn't find a good explanation yet..
7 odpowiedzi
+ 3
x++ is a postfix increment and ++x is prefix. Here's an example. (Not good at JS) So I'll use C++, but you should understand it well.
int x = 6
cout << x++;
The output is 6 because you printed the variable and incremented after, later in the code it be 7 unless changed though.
int x = 6
cout << ++x;
The output is 7 because you incremented the variable before printing it.
Hope this help with ++x and x++, not sure how to answer x+=1 though.
+ 3
Thank you for the explanation!
+ 2
x+=1 is the same as x=x+1
++x; //prefix
x++; //postfix
Prefix increments the value, and then proceeds with the expression.
Postfix evaluates the expression and then performs the incrementing.
+ 2
😀👍
+ 2
@Ash Ketchum I don't believe c++ and js incorporate the pre decriment operator equally.
+ 2
Interesting that I still use this question (and answers of course) as a reference :)
+ 1
@Paul Np, happy to help 😁😁