An assignment operator assigns a value to its left operand based on the value of its right operand.
The simple assignment operator is equal (=
), which assigns the value of its right operand to its left operand.
That is, x = f()
is an assignment expression that assigns the value of f()
to x
.
There are also compound assignment operators that are shorthand for the operations listed in the following table:
Name | Shorthand operator | Meaning |
---|---|---|
Assignment | x = f() |
x = f() |
Addition assignment | x += f() |
x = x + f() |
Subtraction assignment | x -= f() |
x = x - f() |
Multiplication assignment | x *= f() |
x = x * f() |
Division assignment | x /= f() |
x = x / f() |
Remainder assignment | x %= f() |
x = x % f() |
Exponentiation assignment | x **= f() |
x = x ** f() |
Left shift assignment | x <<= f() |
x = x << f() |
Right shift assignment | x >>= f() |
x = x >> f() |
Unsigned right shift assignment | x >>>= f() |
x = x >>> f() |
Bitwise AND assignment | x &= f() |
x = x & f() |
Bitwise XOR assignment | x ^= f() |
x = x ^ f() |
Bitwise OR assignment | x |= f() |
x = x | f() |
Logical AND assignment | x &&= f() |
x && (x = f()) |
Logical OR assignment | x ||= f() |
x || (x = f()) |
Nullish coalescing assignment | x ??= f() |
x ?? (x = f()) |