0
What is x+=1 in js?
I know what is x=+1, but what is x+=1?
7 Réponses
+ 3
x=x+1
+ 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
+ 1
Adding 1 to the current value of x
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!
0
x=+1
x= (+1) positive 1.
you can use - for negative
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.
- 1
why then there are x=+1 and x+=1 what’s the difference?