In JavaScript, assignment operators are used to assign values to variables with less verbosity. The basic assignment operator is =, which assigns the value of its right operand to its left operand. Besides the basic assignment, there are several compound assignment operators that perform an operation and assignment in one step. Here's an overview of the assignment operators in JavaScript:
Basic Assignment Operator
- Assignment (
=): Assigns the value of the right operand to the left operand.let x = 5;
Compound Assignment Operators
-
Addition assignment (
+=): Adds the right operand to the left operand and assigns the result to the left operand.let x = 10; x += 5; // Same as x = x + 5; Now x is 15. -
Subtraction assignment (
-=): Subtracts the right operand from the left operand and assigns the result to the left operand.let x = 10; x -= 5; // Same as x = x - 5; Now x is 5. -
Multiplication assignment (
*=): Multiplies the left operand by the right operand and assigns the result to the left operand.let x = 10; x *= 5; // Same as x = x * 5; Now x is 50. -
Division assignment (
/=): Divides the left operand by the right operand and assigns the result to the left operand.let x = 10; x /= 5; // Same as x = x / 5; Now x is 2. -
Remainder assignment (
%=): Assigns the remainder of dividing the left operand by the right operand to the left operand.let x = 10; x %= 4; // Same as x = x % 4; Now x is 2. -
Exponentiation assignment (
**=): Raises the left operand to the power of the right operand and assigns the result to the left operand.let x = 10; x **= 2; // Same as x = x ** 2; Now x is 100. -
Left shift assignment (
<<=): Shifts the left operand left by the number of bits specified in the right operand and assigns the result to the left operand.let x = 10; x <<= 2; // Same as x = x << 2; Now x is 40. -
Right shift assignment (
>>=): Shifts the left operand right by the number of bits specified in the right operand, maintaining the sign, and assigns the result to the left operand.let x = 10; x >>= 2; // Same as x = x >> 2; Now x is 2. -
Unsigned right shift assignment (
>>>=): Shifts the left operand right by the number of bits specified in the right operand, filling the left with zeros, and assigns the result to the left operand.let x = -10; x >>>= 2; // Performs unsigned right shift and assigns the result -
Bitwise AND assignment (
&=): Performs a bitwise AND operation between the two operands and assigns the result to the left operand.let x = 10; x &= 5; // Same as x = x & 5; Now x is 0. -
Bitwise XOR assignment (
^=): Performs a bitwise XOR operation between the two operands and assigns the result to the left operand.let x = 10; x ^= 5; // Same as x = x ^ 5; Now x is 15. -
Bitwise OR assignment (
|=): Performs a bitwise OR operation between the two operands and assigns the result to the left operand.let x = 10; x |= 5; // Same as x = x | 5; Now x is 15.
These compound assignment operators help to write more concise and readable code, especially when performing operations on the same variable. They are widely used in programming tasks ranging from simple arithmetic calculations to complex bitwise operations.