Assignment operators in JavaScript assign values to variables.
Operator |
Example |
Same As |
= |
x = y |
x = y |
+= |
x += y |
x = x + y |
-= |
x -= y |
x = x – y |
*= |
x *= y |
x = x * y |
/= |
x /= y |
x = x / y |
%= |
x %= y |
x = x % y |
**= |
x **= y |
x = x ** y |
Operator |
Example |
Same As |
<<= |
x <<= y |
x = x << y |
>>= |
x >>= y |
x = x >> y |
>>>= |
x >>>= y |
x = x >>> y |
Operator |
Example |
Same As |
&= |
x &= y |
x = x & y |
^= |
x ^= y |
x = x ^ y |
|= |
x |= y |
x = x | y |
Operator |
Example |
Same As |
&&= |
x &&= y |
x = x && (x = y) |
||= |
x ||= y |
x = x || (x = y) |
??= |
x ??= y |
x = x ?? (x = y) |
Note: The Logical assignment operators were introduced in ES2020. |
The Simple Assignment Operator assigns a value to a variable in JavaScript.
let x = 10; |
let x = 10 + y; |
let x = 10; x += 5; |
let text = “Hello”; text += ” World”; |
The Subtraction Assignment Operator deducts a value from a variable in JavaScript.
let x = 10; x -= 5; |
The Multiplication Assignment Operator scales a variable by a specified value in JavaScript.
let x = 10; x *= 5; |
The Exponentiation Assignment Operator computes the power of a variable by the operand in JavaScript.
let x = 10; x **= 5; |
The Division Assignment Operator divides a variable by a specified value in JavaScript.
let x = 10; x /= 5; |
The Remainder Assignment Operator calculates the remainder of a variable and assigns it to another variable in JavaScript.
let x = 10; x %= 5; |
The Left Shift Assignment Operator shifts a variable to the left by a specified number of bits in JavaScript.
let x = –100; x <<= 5; |
The Right Shift Assignment Operator shifts a variable to the right by a specified number of bits, preserving the sign, in JavaScript.
let x = –100; x >>= 5; |
The Unsigned Right Shift Assignment Operator shifts a variable to the right by a specified number of bits, without preserving the sign, in JavaScript.
let x = –100; x >>>= 5; |
The Bitwise AND Assignment Operator performs a bitwise AND operation on two operands and assigns the result to a variable in JavaScript.
let x = 10; x &= 5; |
The Bitwise OR Assignment Operator performs a bitwise OR operation on two operands and assigns the result to a variable in JavaScript.
let x = 10; x |= 5; |
The Bitwise XOR Assignment Operator performs a bitwise XOR operation on two operands and assigns the result to a variable in JavaScript.
let x = 10; x ^= 5; |
The Logical AND assignment operator between two values.
If the first value evaluates to true, it assigns the second value.
let x = 10; x &&= 5; |
The &&= operator is a feature introduced in ES2020. |
The Logical OR assignment operator between two values.
If the first value evaluates to false, it assigns the second value.
let x = 10; x ||= 5; |
The ||= operator is a feature introduced in ES2020. |
The Nullish coalescing assignment operator between two values.
If the first value is undefined or null, it assigns the second value.
let x; x ??= 5; |
The ??= operator is a feature introduced in ES2020. |