In this chapter, we will learn about JavaScript assignment operators. These operators are used to assign values to variables. We will cover:
- Assignment (=)
- Addition Assignment (+=)
- Subtraction Assignment (-=)
- Multiplication Assignment (*=)
- Division Assignment (/=)
- Modulus Assignment (%=)
- Exponentiation Assignment (**=)
- Left Shift Assignment (<<=)
- Right Shift Assignment (>>=)
- Unsigned Right Shift Assignment (>>>=)
- Bitwise AND Assignment (&=)
- Bitwise OR Assignment (|=)
- Bitwise XOR Assignment (^=)
Assignment (=)
The assignment operator assigns a value to a variable.
Syntax
variable = value;
Example
let x = 10;
console.log(x); // Output: 10
Addition Assignment (+=)
The addition assignment operator adds a value to a variable and assigns the result to the variable.
Syntax
variable += value;
Example
let x = 10;
x += 5;
console.log(x); // Output: 15
Subtraction Assignment (-=)
The subtraction assignment operator subtracts a value from a variable and assigns the result to the variable.
Syntax
variable -= value;
Example
let x = 10;
x -= 5;
console.log(x); // Output: 5
Multiplication Assignment (*=)
The multiplication assignment operator multiplies a variable by a value and assigns the result to the variable.
Syntax
variable *= value;
Example
let x = 10;
x *= 5;
console.log(x); // Output: 50
Division Assignment (/=)
The division assignment operator divides a variable by a value and assigns the result to the variable.
Syntax
variable /= value;
Example
let x = 10;
x /= 5;
console.log(x); // Output: 2
Modulus Assignment (%=)
The modulus assignment operator takes the remainder of a variable divided by a value and assigns the result to the variable.
Syntax
variable %= value;
Example
let x = 10;
x %= 3;
console.log(x); // Output: 1
Exponentiation Assignment (**=)
The exponentiation assignment operator raises a variable to the power of a value and assigns the result to the variable.
Syntax
variable **= value;
Example
let x = 2;
x **= 3;
console.log(x); // Output: 8
Left Shift Assignment (<<=)
The left shift assignment operator shifts a variable’s bits to the left by a specified number of positions and assigns the result to the variable.
Syntax
variable <<= value;
Example
let x = 5; // 5 is 00000101 in binary
x <<= 2; // Shifts bits left by 2 positions: 00010100
console.log(x); // Output: 20
Right Shift Assignment (>>=)
The right shift assignment operator shifts a variable’s bits to the right by a specified number of positions and assigns the result to the variable.
Syntax
variable >>= value;
Example
let x = 20; // 20 is 00010100 in binary
x >>= 2; // Shifts bits right by 2 positions: 00000101
console.log(x); // Output: 5
Unsigned Right Shift Assignment (>>>=)
The unsigned right shift assignment operator shifts a variable’s bits to the right by a specified number of positions (filling with zeros) and assigns the result to the variable.
Syntax
variable >>>= value;
Example
let x = -20; // -20 is 11101100 in binary (2's complement)
x >>>= 2; // Shifts bits right by 2 positions, filling with zeros: 00111011
console.log(x); // Output: 1073741819
Bitwise AND Assignment (&=)
The bitwise AND assignment operator performs a bitwise AND operation between a variable and a value and assigns the result to the variable.
Syntax
variable &= value;
Example
let x = 5; // 5 is 00000101 in binary
x &= 3; // 3 is 00000011 in binary, result is 00000001
console.log(x); // Output: 1
Bitwise OR Assignment (|=)
The bitwise OR assignment operator performs a bitwise OR operation between a variable and a value and assigns the result to the variable.
Syntax
variable |= value;
Example
let x = 5; // 5 is 00000101 in binary
x |= 3; // 3 is 00000011 in binary, result is 00000111
console.log(x); // Output: 7
Bitwise XOR Assignment (^=)
The bitwise XOR assignment operator performs a bitwise XOR operation between a variable and a value and assigns the result to the variable.
Syntax
variable ^= value;
Example
let x = 5; // 5 is 00000101 in binary
x ^= 3; // 3 is 00000011 in binary, result is 00000110
console.log(x); // Output: 6
Conclusion
In this chapter, you learned about JavaScript assignment operators, including basic assignment, addition assignment, subtraction assignment, multiplication assignment, division assignment, modulus assignment, exponentiation assignment, and various bitwise assignment operators. These operators are essential for performing arithmetic and bitwise operations while updating variable values efficiently. In the next chapter, we will explore JavaScript comparison operators and how to use them in your programs.