JavaScript assignment operators are used to assign values to variables. These operators not only assign values but also can perform arithmetic or bitwise operations before assigning the value. Understanding how to use assignment operators effectively is crucial for writing efficient and concise JavaScript code. This article covers the various types of assignment operators in JavaScript with detailed explanations and code examples.
1. Basic Assignment Operator (=)
The basic assignment operator assigns a value to a variable.
Example:
let x = 10;
console.log(x); // Outputs: 10
In this example, the value 10 is assigned to the variable x using the = operator.
2. Addition Assignment Operator (+=)
The addition assignment operator adds a value to a variable and assigns the result to that variable.
Example:
let x = 10;
x += 5; // Equivalent to x = x + 5
console.log(x); // Outputs: 15
In this example, 5 is added to the value of x, and the result 15 is assigned back to x.
3. Subtraction Assignment Operator (-=)
The subtraction assignment operator subtracts a value from a variable and assigns the result to that variable.
Example:
let x = 10;
x -= 5; // Equivalent to x = x - 5
console.log(x); // Outputs: 5
In this example, 5 is subtracted from the value of x, and the result 5 is assigned back to x.
4. Multiplication Assignment Operator (*=)
The multiplication assignment operator multiplies a variable by a value and assigns the result to that variable.
Example:
let x = 10;
x *= 5; // Equivalent to x = x * 5
console.log(x); // Outputs: 50
In this example, the value of x is multiplied by 5, and the result 50 is assigned back to x.
5. Division Assignment Operator (/=)
The division assignment operator divides a variable by a value and assigns the result to that variable.
Example:
let x = 10;
x /= 5; // Equivalent to x = x / 5
console.log(x); // Outputs: 2
In this example, the value of x is divided by 5, and the result 2 is assigned back to x.
6. Modulus Assignment Operator (%=)
The modulus assignment operator applies the modulus operation to a variable and a value, and assigns the result to that variable. The modulus operation returns the remainder of a division.
Example:
let x = 10;
x %= 3; // Equivalent to x = x % 3
console.log(x); // Outputs: 1
In this example, the value of x is divided by 3, and the remainder 1 is assigned back to x.
7. Exponentiation Assignment Operator (**=)
The exponentiation assignment operator raises a variable to the power of a value and assigns the result to that variable.
Example:
let x = 2;
x **= 3; // Equivalent to x = x ** 3
console.log(x); // Outputs: 8
In this example, the value of x is raised to the power of 3, and the result 8 is assigned back to x.
8. Bitwise AND Assignment Operator (&=)
The bitwise AND assignment operator applies the bitwise AND operation to a variable and a value, and assigns the result to that variable.
Example:
let x = 5; // 0101 in binary
x &= 3; // 0011 in binary
console.log(x); // Outputs: 1 (0001 in binary)
In this example, the bitwise AND operation is performed on the binary representations of x and 3, and the result 1 is assigned back to x.
9. Bitwise OR Assignment Operator (|=)
The bitwise OR assignment operator applies the bitwise OR operation to a variable and a value, and assigns the result to that variable.
Example:
let x = 5; // 0101 in binary
x |= 3; // 0011 in binary
console.log(x); // Outputs: 7 (0111 in binary)
In this example, the bitwise OR operation is performed on the binary representations of x and 3, and the result 7 is assigned back to x.
10. Bitwise XOR Assignment Operator (^=)
The bitwise XOR assignment operator applies the bitwise XOR operation to a variable and a value, and assigns the result to that variable.
Example:
let x = 5; // 0101 in binary
x ^= 3; // 0011 in binary
console.log(x); // Outputs: 6 (0110 in binary)
In this example, the bitwise XOR operation is performed on the binary representations of x and 3, and the result 6 is assigned back to x.
11. Bitwise Left Shift Assignment Operator (<<=)
The bitwise left shift assignment operator shifts the bits of a variable to the left by a specified number of positions and assigns the result to that variable.
Example:
let x = 5; // 0101 in binary
x <<= 1; // Shifts bits to the left by 1 position
console.log(x); // Outputs: 10 (1010 in binary)
In this example, the bits of x are shifted to the left by 1 position, and the result 10 is assigned back to x.
12. Bitwise Right Shift Assignment Operator (>>=)
The bitwise right shift assignment operator shifts the bits of a variable to the right by a specified number of positions and assigns the result to that variable.
Example:
let x = 5; // 0101 in binary
x >>= 1; // Shifts bits to the right by 1 position
console.log(x); // Outputs: 2 (0010 in binary)
In this example, the bits of x are shifted to the right by 1 position, and the result 2 is assigned back to x.
13. Bitwise Unsigned Right Shift Assignment Operator (>>>=)
The bitwise unsigned right shift assignment operator shifts the bits of a variable to the right by a specified number of positions, filling the leftmost bits with zeros, and assigns the result to that variable.
Example:
let x = -5; // In binary: 11111111111111111111111111111011
x >>>= 1; // Shifts bits to the right by 1 position, fills with 0
console.log(x); // Outputs: 2147483645
In this example, the bits of x are shifted to the right by 1 position, filling the leftmost bits with zeros, and the result 2147483645 is assigned back to x.