Menu Close

JavaScript Assignment Operators

Posted in Javascript Tutorial

JavaScript provides a variety of assignment operators that allow you to assign values to variables in different ways. Here is a list of assignment operators along with examples of their usage:

  • Assignment (=) Operator: Assigns the value on the right to the variable on the left.
    let x = 10; // Assign the value 10 to the variable x
    
  • Add and Assign (+=) Operator: Adds the value on the right to the variable’s current value and assigns the result to the variable.
    let y = 5;
    y += 3; // Equivalent to y = y + 3; (y is now 8)
    
  • Subtract and Assign (-=) Operator: Subtracts the value on the right from the variable’s current value and assigns the result to the variable.
    let z = 7;
    z -= 2; // Equivalent to z = z - 2; (z is now 5)
    
  • Multiply and Assign (*=) Operator: Multiplies the variable’s current value by the value on the right and assigns the result to the variable.
    let a = 4;
    a *= 6; // Equivalent to a = a * 6; (a is now 24)
    
  • Divide and Assign (/=) Operator: Divides the variable’s current value by the value on the right and assigns the result to the variable.
    let b = 12;
    b /= 3; // Equivalent to b = b / 3; (b is now 4)
    
  • Modulus and Assign (%=) Operator: Calculates the remainder when the variable’s current value is divided by the value on the right and assigns the result to the variable.
    let c = 17;
    c %= 5; // Equivalent to c = c % 5; (c is now 2)
    
  • Exponentiation and Assign (**=) Operator (ES6): Raises the variable’s current value to the power of the value on the right and assigns the result to the variable.
    let d = 2;
    d **= 3; // Equivalent to d = d ** 3; (d is now 8)
    

Left Shift Assignment Operator

The “Left Shift and Assign” operator (<<=) is a JavaScript assignment operator that shifts the bits of a variable’s binary representation to the left by a specified number of positions and assigns the result back to the variable. It’s used for bitwise left shifting and assignment.

Here’s an example of the left shift assignment operator in action:

let number = 12; // Binary representation: 1100

number <<= 2; // Left-shift the binary representation by 2 positions

console.log(number); // Result: 48 (Binary representation: 110000)

Right Shift Assignment Operator

In JavaScript, the right shift assignment operator is represented by >>=. It is a compound assignment operator that combines the right shift (>>) operator with assignment (=).

The right shift operator (>>) is used to shift the bits of a binary number to the right by a specified number of positions. When combined with the assignment operator, it allows you to shift the bits of a variable’s value to the right and assign the result back to the same variable.

Here’s a basic usage example of the right shift assignment operator:

let x = 12;  // Binary: 1100
x >>= 2;     // Right shift by 2 positions: 0011 (Decimal: 3)
console.log(x); // Output: 3

In this example, we start with the variable x containing the value 12 (binary 1100). We then use the >>= operator to right-shift the bits of x by 2 positions. After the operation, x is updated to 3 (binary 0011), and that’s what gets printed to the console.

The right shift assignment operator is often used when you need to divide an integer by a power of 2 or perform other bitwise operations on a variable while simultaneously updating its value.

Bitwise AND Assignment Operator in JavaScript

In JavaScript, the bitwise AND assignment operator is represented by &=. It is a compound assignment operator that combines the bitwise AND (&) operator with assignment (=).

The bitwise AND operator (&) is used to perform a bitwise “and” operation between the individual bits of two binary numbers. When combined with the assignment operator, it allows you to perform a bitwise “and” operation between a variable’s value and another value, and then assign the result back to the same variable.

Here’s a basic usage example of the bitwise AND assignment operator:

let x = 12;  // Binary: 1100
x &= 7;      // Bitwise AND with 7 (Binary: 0111)
console.log(x); // Output: 4 (Binary: 0100)

In this example, we start with the variable x containing the value 12 (binary 1100). We then use the &= operator to perform a bitwise “and” operation between the value of x and 7 (binary 0111). After the operation, x is updated to 4 (binary 0100), and that’s what gets printed to the console.

The bitwise AND assignment operator is often used in situations where you need to manipulate individual bits in a variable, clear specific bits, or perform other bitwise operations while simultaneously updating its value.

Bitwise OR Assignment Operator in JavaScript

In JavaScript, the bitwise OR assignment operator is represented by |=. It is a compound assignment operator that combines the bitwise OR (|) operator with assignment (=).

The bitwise OR operator (|) is used to perform a bitwise “or” operation between the individual bits of two binary numbers. When combined with the assignment operator, it allows you to perform a bitwise “or” operation between a variable’s value and another value and then assign the result back to the same variable.

Here’s a basic usage example of the bitwise OR assignment operator:

let x = 5;   // Binary: 0101
x |= 3;      // Bitwise OR with 3 (Binary: 0011)
console.log(x); // Output: 7 (Binary: 0111)

In this example, we start with the variable x containing the value 5 (binary 0101). We then use the |= operator to perform a bitwise “or” operation between the value of x and 3 (binary 0011). After the operation, x is updated to 7 (binary 0111), and that’s what gets printed to the console.

The bitwise OR assignment operator is often used in situations where you need to combine or set specific bits in a variable, perform other bitwise operations while simultaneously updating its value, or create bit masks for various purposes.

Bitwise XOR Assignment  Operator in JavaScript

In JavaScript, the bitwise XOR assignment operator is represented by ^=. It is a compound assignment operator that combines the bitwise XOR (^) operator with assignment (=).

The bitwise XOR operator (^) is used to perform a bitwise “exclusive or” operation between the individual bits of two binary numbers. When combined with the assignment operator, it allows you to perform a bitwise XOR operation between a variable’s value and another value, and then assign the result back to the same variable.

Here’s a basic usage example of the bitwise XOR assignment operator:

let x = 10;  // Binary: 1010
x ^= 6;      // Bitwise XOR with 6 (Binary: 0110)
console.log(x); // Output: 12 (Binary: 1100)

In this example, we start with the variable x containing the value 10 (binary 1010). We then use the ^= operator to perform a bitwise XOR operation between the value of x and 6 (binary 0110). After the operation, x is updated to 12 (binary 1100), and that’s what gets printed to the console.

The bitwise XOR assignment operator is often used in situations where you need to toggle or invert specific bits in a variable, perform other bitwise operations while simultaneously updating its value, or create bit masks for various purposes.

 

Leave a Reply