Assignment operators assign values to JavaScript variables.
Operator | Example | Same As | Demonstration |
---|---|---|---|
= | x = y | x = y | The assignment operator assigns a value to the variable named x:
var x = 10; (The value of x is 10) |
+= | x += y | x = x + y | The addition assignment operator adds a value to the variable named x:
var x = 10; x += 5; (The value of x is 15) |
-= | x -= y | x = x - y | The subtraction assignment operator subtracts a value from the variable named x:
var x = 10; x -= 5; (The value of x is 5) |
*= | x *= y | x = x * y | The multiplication assignment operator multiplies a value with the variable named x:
var x = 10; x *= 5; (The value of x is 50) |
/= | x /= y | x = x / y | The division assignment operator divides a value in the variable named x:
var x = 10; x /= 5; (The value of x is 2) |
%= | x %= y | x = x % y | The modulus assignment operator computes the remainder from the division of the numbers:
var x = 10; x %= 5; (10 ÷ 5 = 2 means nothing is leftover, so the value of x is 0) |