What is x+=1 in js? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is x+=1 in js?

I know what is x=+1, but what is x+=1?

5th Oct 2019, 1:08 PM
Andrii Gorokhovskyi
Andrii Gorokhovskyi - avatar
7 Answers
+ 3
x=x+1
6th Oct 2019, 1:23 AM
Sonic
Sonic - avatar
+ 1
x = 2 assining x the value 2 x += 2 adding 2 to the current value of x x++ increasing the value of x by one if x is inside an operation of sorts the value gets used first then it's modified ++x increasing the value of x by one if x is inside an operation of sorts the value gets modified first then the new value gets used
5th Oct 2019, 1:13 PM
Anton Böhler
Anton Böhler - avatar
+ 1
Adding 1 to the current value of x
7th Oct 2019, 4:07 AM
Mohib
Mohib - avatar
0
x+=1 is a compound assignment, the word "compound" here tells you that it does more than just assignment. It is functionally equivalent to x=x+1 or x=1+x or ++x https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators Whenever you see something new, please do your best to search it up before asking anywhere. Thanks!
5th Oct 2019, 1:27 PM
jtrh
jtrh - avatar
0
x=+1 x= (+1) positive 1. you can use - for negative
5th Oct 2019, 1:40 PM
Taste
Taste - avatar
0
The += is called "addition assignment operator". x += 1 would be assigning x to the value of x + 1. Example. var x = 5; x += 3; document.write(x); // This would return 8. Another Example. var x = 5; var y = 10; var z; y += x; //This would assign y a new value of 15. z = y += x;// This assigns the variable z to the values of variables y(15) and x(5) document.write(z); // This would return 20.
5th Oct 2019, 3:00 PM
Anthony Johnson
Anthony Johnson - avatar
- 1
why then there are x=+1 and x+=1 what’s the difference?
5th Oct 2019, 1:35 PM
Andrii Gorokhovskyi
Andrii Gorokhovskyi - avatar