Assignment operators assign values to JavaScript 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) |
The Logical assignment operators are ES2020.
The Simple Assignment Operator assigns a value to a variable.
The Addition Assignment Operator adds a value to a variable.
let x = 10;
x += 5;
Try it Yourself »
let text = "Hello"; text += " World";
Try it Yourself »
The Subtraction Assignment Operator subtracts a value from a variable.
The Multiplication Assignment Operator multiplies a variable.
The Exponentiation Assignment Operator raises a variable to the power of the operand.
The Division Assignment Operator divides a variable.
The Remainder Assignment Operator assigns a remainder to a variable.
The Left Shift Assignment Operator left shifts a variable.
The Right Shift Assignment Operator right shifts a variable (signed).
The Unsigned Right Shift Assignment Operator right shifts a variable (unsigned).
The Bitwise AND Assignment Operator does a bitwise AND operation on two operands and assigns the result to the the variable.
The Bitwise OR Assignment Operator does a bitwise OR operation on two operands and assigns the result to the variable.
The Bitwise XOR Assignment Operator does a bitwise XOR operation on two operands and assigns the result to the variable.
The Logical AND assignment operator is used between two values.
If the first value is true, the second value is assigned.
The &&=
operator is an ES2020 feature.
The Logical OR assignment operator is used between two values.
If the first value is false, the second value is assigned.
The ||=
operator is an ES2020 feature.
The Nullish coalescing assignment operator is used between two values.
If the first value is undefined or null, the second value is assigned.
The ??=
operator is an ES2020 feature.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!