In JavaScript, Assignment Operators are used to assign values to variables. These operators can also perform operations and assign the result in one step, which makes them useful for modifying variables while updating their values.
List of Assignment Operators:
Operator | Description | Example | Result |
---|---|---|---|
= |
Simple assignment | x = 5 |
x = 5 |
+= |
Addition assignment | x += 3 |
x = x + 3 |
-= |
Subtraction assignment | x -= 2 |
x = x - 2 |
*= |
Multiplication assignment | x *= 2 |
x = x * 2 |
/= |
Division assignment | x /= 2 |
x = x / 2 |
%= |
Modulus assignment | x %= 2 |
x = x % 2 |
**= |
Exponentiation assignment | x **= 2 |
x = x ** 2 |
<<= |
Left shift assignment | x <<= 2 |
x = x << 2 |
>>= |
Right shift assignment | x >>= 2 |
x = x >> 2 |
&= |
Bitwise AND assignment | x &= 2 |
x = x & 2 |
` | =` | Bitwise OR assignment | `x |
^= |
Bitwise XOR assignment | x ^= 2 |
x = x ^ 2 |
~= |
Bitwise NOT assignment | x ~= 2 |
x = ~x |
&&= |
Logical AND assignment | x &&= true |
x = x && true |
` | =` | Logical OR assignment |
1. Simple Assignment (=
)
The most basic assignment operator, which assigns a value to a variable.
Example:
let x = 10; console.log(x); // 10
2. Addition Assignment (+=
)
Adds the value on the right to the variable on the left, and assigns the result to the variable.
Example:
let x = 10; x += 5; // Equivalent to x = x + 5 console.log(x); // 15
3. Subtraction Assignment (-=
)
Subtracts the value on the right from the variable on the left, and assigns the result to the variable.
Example:
let x = 10; x -= 3; // Equivalent to x = x - 3 console.log(x); // 7
4. Multiplication Assignment (*=
)
Multiplies the variable on the left by the value on the right, and assigns the result to the variable.
Example:
let x = 5; x *= 4; // Equivalent to x = x * 4 console.log(x); // 20
5. Division Assignment (/=
)
Divides the variable on the left by the value on the right, and assigns the result to the variable.
Example:
let x = 20; x /= 4; // Equivalent to x = x / 4 console.log(x); // 5
6. Modulus Assignment (%=
)
Assigns the remainder of dividing the variable on the left by the value on the right.
Example:
let x = 20; x %= 3; // Equivalent to x = x % 3 console.log(x); // 2 (remainder of 20 / 3)
7. Exponentiation Assignment (**=
)
Raises the variable on the left to the power of the value on the right, and assigns the result to the variable.
Example:
let x = 2; x **= 3; // Equivalent to x = x ** 3 console.log(x); // 8 (2^3)
8. Bitwise Shift Operators (<<=
, >>=
)
Used for shifting bits to the left or right.
<<=
: Left shift>>=
: Right shift
Example:
let x = 4; // 0100 in binary x <<= 1; // Equivalent to x = x << 1 console.log(x); // 8 (1000 in binary) let y = 8; // 1000 in binary y >>= 1; // Equivalent to y = y >> 1 console.log(y); // 4 (0100 in binary)
9. Bitwise AND Assignment (&=
)
Performs a bitwise AND between the variable on the left and the value on the right, and assigns the result to the variable.
Example:
let x = 5; // 0101 in binary x &= 3; // 0011 in binary, x = x & 3 console.log(x); // 1 (0001 in binary)
10. Bitwise OR Assignment (|=
)
Performs a bitwise OR between the variable on the left and the value on the right, and assigns the result to the variable.
Example:
let x = 5; // 0101 in binary x |= 3; // 0011 in binary, x = x | 3 console.log(x); // 7 (0111 in binary)
11. Bitwise XOR Assignment (^=
)
Performs a bitwise XOR between the variable on the left and the value on the right, and assigns the result to the variable.
Example:
let x = 5; // 0101 in binary x ^= 3; // 0011 in binary, x = x ^ 3 console.log(x); // 6 (0110 in binary)
12. Logical AND Assignment (&&=
)
Performs a logical AND between the variable on the left and the value on the right, and assigns the result to the variable.
Example:
let x = true; x &&= false; // Equivalent to x = x && false console.log(x); // false
13. Logical OR Assignment (||=
)
Performs a logical OR between the variable on the left and the value on the right, and assigns the result to the variable.
Example:
let x = false; x ||= true; // Equivalent to x = x || true console.log(x); // true
Summary
Assignment operators help in simplifying expressions and combining operations with assignment in one step. Using operators like +=
, -=
, *=
, and others improves readability and efficiency in modifying variable values.