JavaScript » Assignment

= Operator


The basic assignment operator is the equal sign, which assigns the value (literal or variable) on its right to the variable on its left.
The assignment operator can also be combined with a variety of other operators to provide shorthand versions of standard operations.

Examples

Code:
x = a;
Explanation:

Code:
a += b instead of a = a + b
a -= b instead of a = a - b
a *= b instead of a = a * b
a /= b instead of a = a / b
a %= b instead of a = a % b
Explanation:

Assignment operator combines with the arithmetic operators +, -, *, / and % to give the above shorthand versions

Code:
a <<= b instead of a = a << b
a >>= b instead of a = a >> b
a >>>= b instead of a = a >>> b
a &= b instead of a = a & b
a ^= b instead of a = a ^ b
a |= b instead of a = a | b
Explanation:

It also combines with the above bitwise operators

NOTE:
If the operand to the left of the equal sign itself contains an assignment operator, then the above shorthand statements won't work. Instead you must use the longer version of the statement.