Wednesday 6 November 2024

JavaScript Compound Assignment Operators: A Guide with Examples

What Are Compound Assignment Operators?

Compound assignment operators in JavaScript combine a basic arithmetic or bitwise operation with assignment. They provide a shorthand way to update the value of a variable by performing an operation on it and then assigning the result back to the same variable.

 

List of Compound Assignment Operators

Here are the common compound assignment operators in JavaScript:

 

·      Addition Assignment (+=)

·      Subtraction Assignment (-=)

·      Multiplication Assignment (*=)

·      Division Assignment (/=)

·      Remainder Assignment (%=)

·      Exponentiation Assignment (**=)

·      Left Shift Assignment (<<=)

·      Right Shift Assignment (>>=)

·      Unsigned Right Shift Assignment (>>>=)

·      Bitwise AND Assignment (&=)

·      Bitwise OR Assignment (|=)

·      Bitwise XOR Assignment (^=)

 

Detailed Explanation with Examples

1. Addition Assignment (+=)

This operator adds the value on the right to the variable on the left and assigns the result to the variable.

let x = 5;
x += 3; // Equivalent to x = x + 3;
console.log(x); // Output: 8

 

2. Subtraction Assignment (-=)

This operator subtracts the value on the right from the variable on the left and assigns the result to the variable.

let x = 10;
x -= 4; // Equivalent to x = x - 4;
console.log(x); // Output: 6

 

3. Multiplication Assignment (*=)

This operator multiplies the variable on the left by the value on the right and assigns the result to the variable.

let x = 7;
x *= 2; // Equivalent to x = x * 2;
console.log(x); // Output: 14

 

4. Division Assignment (/=)

This operator divides the variable on the left by the value on the right and assigns the result to the variable.

let x = 20;
x /= 5; // Equivalent to x = x / 5;
console.log(x); // Output: 4

 

5. Remainder Assignment (%=)

This operator calculates the remainder of dividing the variable on the left by the value on the right and assigns the result to the variable.

let x = 15;
x %= 4; // Equivalent to x = x % 4;
console.log(x); // Output: 3

 

6. Exponentiation Assignment (**=)

This operator raises the variable on the left to the power of the value on the right and assigns the result to the variable.

let x = 3;
x **= 2; // Equivalent to x = x ** 2;
console.log(x); // Output: 9

 

7. Left Shift Assignment (<<=)

This operator shifts the bits of the variable on the left by the specified number of places to the left and assigns the result to the variable.

let x = 5; // Binary: 0101
x <<= 1;  // Equivalent to x = x << 1;
console.log(x); // Output: 10 (Binary: 1010)

 

8. Right Shift Assignment (>>=)

This operator shifts the bits of the variable on the left by the specified number of places to the right and assigns the result to the variable.

let x = 20; // Binary: 10100
x >>= 2;   // Equivalent to x = x >> 2;
console.log(x); // Output: 5 (Binary: 0101)

 

9. Unsigned Right Shift Assignment (>>>=)

This operator shifts the bits of the variable on the left by the specified number of places to the right, filling in zeros from the left, and assigns the result to the variable.

 

let x = -20; // Binary: 11111111111111111111111111101100 (32-bit representation)
x >>>= 2;   // Equivalent to x = x >>> 2;
console.log(x); // Output: 1073741819 (Binary: 001111111111111111111111111011)

 

10. Bitwise AND Assignment (&=)

This operator performs a bitwise AND operation on the variable on the left and the value on the right, then assigns the result to the variable.

let x = 6; // Binary: 0110
x &= 3;    // Equivalent to x = x & 3;
console.log(x); // Output: 2 (Binary: 0010)

11. Bitwise OR Assignment (|=)

This operator performs a bitwise OR operation on the variable on the left and the value on the right, then assigns the result to the variable.

let x = 6; // Binary: 0110
x |= 3;    // Equivalent to x = x | 3;
console.log(x); // Output: 7 (Binary: 0111)

12. Bitwise XOR Assignment (^=)

This operator performs a bitwise XOR operation on the variable on the left and the value on the right, then assigns the result to the variable.

let x = 6; // Binary: 0110
x ^= 3;    // Equivalent to x = x ^ 3;
console.log(x); // Output: 5 (Binary: 0101)

Advantages of Compound Assignment Operators

1.   Conciseness: They reduce the amount of code you need to write, making it more readable and concise.

2.   Efficiency: They perform the operation and assignment in a single step, which can be more efficient in some cases.

3.   Clarity: They make it clear that the operation modifies the variable itself, which can help with understanding the code.

 

sumOfNumbers.js

let sum = 0;
for (let i = 1; i <= 5; i++) {
    sum += i; // Equivalent to sum = sum + i;
}
console.log(sum); // Output: 15


 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment