The assignment operators of JavaScript are: "=", "=", "-=", "*=", "/=", "%=", "<<=", " >>=", ">>>=", "&=", "|=", "^=".
The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.
In JavaScript, the assignment operator is used to assign values to variables. The operand on the left side of an assignment operator must be a variable, object property, or array element, also known as an lvalue.
For example, the following writing is wrong because the value on the left is a fixed value and operations are not allowed.
1 = 100; //返回错误
The assignment operation has the following two forms:
Simple assignment operation =
: Directly copy the value of the operand on the right side of the equal sign to the left operand, so the value of the left operand changes.
Assignment operation of additional operations: Before assignment, perform some operation on the right operand, and then copy the operation result to the left operand. The specific instructions are as shown in the table:
Assignment operator | Explanation | Example | Equivalent to |
---|---|---|---|
= | Addition or concatenation operation and assignment | a = b | a = a b |
-= | Subtraction operation and assignment | a -= b | a= a - b |
*= | Multiplication and assignment | a *= b | a = a * b |
/= | Division operation and assignment | a /= b | a = a / b |
%= | Modulo operation and assignment | a %= b | a = a % b |
<<= | Left shift operation and assignment | a <<= b | a = a << b |
>>= | Right shift operation and assignment | a >>= b | a = a >> b |
##>>>=
| Unsigned right shift operation and assignment bita >>>= b | a = a >>> b | |
Bitwise AND operation and assignment | a &= b | a = a & b | |
Bitwise OR operation and assignment | a |= ba = a |= b | ##^= | |
Bit XOR operation and assignment a ^= b | a = a ^ b | The sample code is as follows: |