JavaScript Assignment Operators
Assignment operators assign values to variables. JavaScript provides several types of assignment operators to perform different operations. Below are the most commonly used assignment operators along with examples.
1. Basic Assignment
The basic assignment operator (=
) assigns a value to a variable.
Example:
let a = 10;
let b = 5;
console.log(a); // Output: 10
console.log(b); // Output: 5
2. Addition Assignment
The addition assignment operator (+=
) adds a value to a variable and assigns the result to that variable.
Example:
let a = 10;
a += 5; // Equivalent to a = a + 5
console.log(a); // Output: 15
3. Subtraction Assignment
The subtraction assignment operator (-=
) subtracts a value from a variable and assigns the result to that variable.
Example:
let a = 10;
a -= 5; // Equivalent to a = a - 5
console.log(a); // Output: 5
4. Multiplication Assignment
The multiplication assignment operator (*=
) multiplies a variable by a value and assigns the result to that variable.
Example:
let a = 10;
a *= 5; // Equivalent to a = a * 5
console.log(a); // Output: 50
5. Division Assignment
The division assignment operator (/=
) divides a variable by a value and assigns the result to that variable.
Example:let a = 10;
a /= 2; // Equivalent to a = a / 2
console.log(a); // Output: 5
let a = 10;
a /= 2; // Equivalent to a = a / 2
console.log(a); // Output: 5
6. Modulus Assignment
The modulus assignment operator (%=
) divides a variable by a value and assigns the remainder to that variable.
Example:
let a = 10;
a %= 3; // Equivalent to a = a % 3
console.log(a); // Output: 1
7. Exponentiation Assignment
The exponentiation assignment operator (**=
) raises a variable to the power of a value and assigns the result to that variable.
Example:
let a = 2;
a **= 3; // Equivalent to a = a ** 3
console.log(a); // Output: 8
8. Bitwise AND Assignment
The bitwise AND assignment operator (&=
) performs a bitwise AND operation on a variable and a value and assigns the result to that variable.
Example:
let a = 5; // 0101 in binary
a &= 3; // Equivalent to a = a & 3 (0011 in binary)
console.log(a); // Output: 1 (0001 in binary)
9. Bitwise OR Assignment
The bitwise OR assignment operator (|=
) performs a bitwise OR operation on a variable and a value and assigns the result to that variable.
Example:
let a = 5; // 0101 in binary
a |= 3; // Equivalent to a = a | 3 (0011 in binary)
console.log(a); // Output: 7 (0111 in binary)
10. Bitwise XOR Assignment
The bitwise XOR assignment operator (^=
) performs a bitwise XOR operation on a variable and a value and assigns the result to that variable.
Example:
let a = 5; // 0101 in binary
a ^= 3; // Equivalent to a = a ^ 3 (0011 in binary)
console.log(a); // Output: 6 (0110 in binary)
11. Bitwise Left Shift Assignment
The bitwise left shift assignment operator (<<=
) performs a bitwise left shift on a variable and assigns the result to that variable.
Example:
let a = 5; // 0101 in binary
a <<= 1; // Equivalent to a = a << 1 (left shift by 1)
console.log(a); // Output: 10 (1010 in binary)
12. Bitwise Right Shift Assignment
The bitwise right shift assignment operator (>>=
) performs a bitwise right shift on a variable and assigns the result to that variable.
Example:
let a = 5; // 0101 in binary
a >>= 1; // Equivalent to a = a >> 1 (right shift by 1)
console.log(a); // Output: 2 (0010 in binary)
13. Unsigned Right Shift Assignment
The unsigned right shift assignment operator (>>>=
) performs an unsigned right shift on a variable and assigns the result to that variable.
Example:
let a = -5; // 11111111111111111111111111111011 in binary
a >>>= 1; // Equivalent to a = a >>> 1 (unsigned right shift by 1)
console.log(a); // Output: 2147483645 (01111111111111111111111111111101 in binary)
By understanding and using these assignment operators, you can efficiently perform various operations and assignments in your JavaScript code.