Difference between x++ or ++x or x+=1 in JavaScript. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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..

3rd Dec 2016, 11:21 PM
3TW3
3TW3 - avatar
7 Answers
+ 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.
3rd Dec 2016, 11:35 PM
Ash Ketchum
Ash Ketchum - avatar
+ 3
Thank you for the explanation!
3rd Dec 2016, 11:39 PM
3TW3
3TW3 - avatar
+ 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.
3rd Dec 2016, 11:37 PM
TheLastCookie
TheLastCookie - avatar
+ 2
😀👍
3rd Dec 2016, 11:41 PM
3TW3
3TW3 - avatar
+ 2
@Ash Ketchum I don't believe c++ and js incorporate the pre decriment operator equally.
3rd Dec 2016, 11:44 PM
Vincent Kordiak
Vincent Kordiak - avatar
+ 2
Interesting that I still use this question (and answers of course) as a reference :)
15th Aug 2017, 10:34 PM
3TW3
3TW3 - avatar
+ 1
@Paul Np, happy to help 😁😁
3rd Dec 2016, 11:40 PM
Ash Ketchum
Ash Ketchum - avatar