JavaScript Introduction | Operators | Assignment Operators

Assignment Operators

An assignment operator is a binary operator used to assign a value to a variable, and its operands are associated from right to left.
There are also several compound assignment operators that combine assignment with the arithmetic operators introduced earlier.

Assignment operator Description
= Assigns the value of the right operand to the left operand.
+= Adds the value of the right operand to the value of the left operand, then assigns the result to the left operand.
-= Subtracts the value of the right operand from the value of the left operand, then assigns the result to the left operand.
*= Multiplies the value of the left operand by the value of the right operand, then assigns the result to the left operand.
/= Divides the value of the left operand by the value of the right operand, then assigns the result to the left operand.
%= Divides the value of the left operand by the value of the right operand, then assigns the remainder to the left operand.
var x = 10, y = 10, z = 10;
x = x - 5;
y -= 5; // Same expression as y = y - 5.
z =- 5; // Same expression as z = -5.

In the example above, the z =- 5 operation simply assigns -5 to the variable z.
As this shows, the order of operators in compound assignment operators is very important, so you need to pay attention to it.