• JavaScript Video Tutorials

JavaScript - Assignment Operators



JavaScript Assignment Operators

The assignment operators in JavaScript are used to assign values to the variables. These are binary operators. An assignment operator takes two operands, assigns a value to the left operand based on the value of right operand. The left operand is always a variable and the right operand may be literal, variable or expression.

let x = 10; // right operand is a literal
let y = x; // right operand is a variable
let z = x + 10; // right operand is an expression

An assignment operator first evaluates the expression and then assign the value to the variable (left operand).

A simple assignment operator is equal (=) operator. In the JavaScript statement "let x = 10;", the = operator assigns 10 to the variable x.

We can combine a simple assignment operator with other type of operators such as arithmetic, logical, etc. to get compound assignment operators. Some arithmetic assignment operators are +=, -=, *=, /=, etc. The += operator performs addition operation on the operands and assign the result to the left hand operand.

Arithmetic Assignment Operators

In this section, we will cover simple assignment and arithmetic assignment operators. An arithmetic assignment operator performs arithmetic operation and assign the result to a variable. Following is the list of operators with example −

Assignment Operator Example Equivalent To
= (Assignment) a = b a = b
+= (Addition Assignment) a += b a = a + b
-= (Subtraction Assignment) a -= b a = a – b
*= (Multiplication Assignment) a *= b a = a * b
/= (Division Assignment) a /= b a = a / b
%= (Remainder Assignment) a %= b a = a % b
**= (Exponentiation Assignment) a **= b a = a ** b

Simple Assignment (=) Operator

A simple assignment (=) operator assigns a value to a variable. We can assign a single value to multiple variables. This is known as assignment chaining.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 5;
    let y = x + 10; 
    document.getElementById("output").innerHTML = 
    "Value of x : " + x + "<br>" +
    "Value of y : " + y;
  </script>
</body>
</html>

Below is an example of assignment chaining −

<html>
<body>
  <div id="output"></div>
  <script>
    let x = y = 5;
    document.getElementById("output").innerHTML = 
    "Value of x : " + x + "<br>" +
    "Value of y : " + y;
  </script>
</body>
</html>

Addition Assignment (+=) Operator

The JavaScript addition assignment operator performs addition on the two operands and assigns the result to the left operand. Here addition may be numeric addition or string concatenation.

x += b;

In the above statement, it adds values of b and x and assigns the result to x.

Example: Numeric Addition Assignment

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 5;
    x += 7;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Example: String Concatenation Assignment

<html>
<body>
  <div id="output"></div>
  <script>
    let x ="Hello";
    x += " World";
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Subtraction Assignment (-=) Operator

The subtraction assignment operator in JavaScript subtracts the value of right operand from the left operand and assigns the result to left operand (variable).

let x -=b;

In the above statement, it subtracts b from x and assigns the result to x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 15;
    x -= 5;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Multiplication Assignment (*=) Operator

The multiplication assignment operator in JavaScript multiplies the both operands and assign the result to the left operand.

let x *= b;

In the above statement, it multiplies x and b and assigns the result to x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 10;
	x *= 5;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Division Assignment (/=) Operator

This operator divides left operand by the right operand and assigns the result to left operand.

let x /= b;

In the above statement, it divides x by b and assigns the result (quotient) to x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 10;
    x /= 5;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Remainder Assignment (%=) Operator

The JavaScript remainder assignment operator performs the remainder operation on the operands and assigns the result to left operand.

let x %= b;

In the above statement, it divides x by b and assigns the result (remainder) to x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 12;
    x %= 5;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Exponentiation Assignment (**=) Operator

This operator performs exponentiation operation on the operands and assigns the result to left operand.

let x **= b;

In the above statement, it computes x**b and assigns the result to x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 5;
    x **= 3;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

JavaScript Bitwise Assignment operators

A bitwise assignment operator performs bitwise operation on the operands and assign the result to a variable. These operations perform two operations, first a bitwise operation and second the simple assignment operation. Bitwise operation is done on bit-level. A bitwise operator treats both operands as 32-bit signed integers and perform the operation on corresponding bits of the operands. The simple assignment operator assigns result is to the variable (left operand).

Following is the list of operators with example −

Assignment Operator Example Equivalent To
&= (Bitwise AND Assignment) a &= b a = a & b
|= (Bitwise OR Assignment) a |= b a = a | b
^= (Bitwise XOR Assignment) a ^= b a = a ^ b

Bitwise AND Assignment Operator

The JavaScript bitwise AND assignment (&=) operator performs bitwise AND operation on the operands and assigns the result to the left operand (variable).

let x &= b;

In the above statement, it performs bitwise AND on x and b and assigns the result to the variable x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 7; 
    x &= 5;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Bitwise OR Assignment Operator

The JavaScript bitwise OR assignment (|=) operator performs bitwise OR operation on the operands and assigns the result to the left operand (variable).

let x |= b;

In the above statement, it performs bitwise OR on x and b and assigns the result to the variable x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 7; 
    x |= 5;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Bitwise XOR Assignment Operator

The JavaScript bitwise XOR assignment (^=) operator performs bitwise XOR operation on the operands and assigns the result to the left operand (variable).

let x ^= b;

In the above statement, it performs bitwise XOR on x and b and assigns the result to the variable x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 7; 
    x ^= 5;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

JavaScript Shift Assignment Operators

A shift assignment operator performs bitwise shift operation on the operands and assign the result to a variable (left operand). These are a combinations two operators, the first bitwise shift operator and second the simple assignment operator.

Following is the list of the shift assignment operators with example −

Assignment Operator Example Equivalent To
<<= (Left Shift Assignment) a <<= b a = a << b
>>= (Right Shift Assignment) a >>= b a = a >> b
>>>= (Unsigned Right Shift Assignment) a >>>= b a = a >>> b

Left Shift Assignment Operator

The JavaScript left shift assignment (<<=) operator performs left shift operation on the operands and assigns the result to the left operand (variable).

let x <<= b;

In the above statement, it performs left shift on x and b and assigns the result to the variable x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 7; 
    x <<= 2; 
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Right Shift Assignment Operator

The JavaScript right shift assignment (>>=) operator performs right shift operation on the operands and assigns the result to the left operand (variable).

let x >>= b;

In the above statement, it performs right shift on x and b and assigns the result to the variable x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 7; 
	x >>= 1; 
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Unsigned Right Shift Assignment Operator

The JavaScript unsigned right shift assignment (>>>=) operator performs unsigned right shift operation on the operands and assigns the result to the left operand (variable).

let x >>>= b;

In the above statement, it performs unsigned right shift on x and b and assigns the result to the variable x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 7; 
    x >>>= 2;
    document.getElementById("output").innerHTML ="Value of x : " + x;
  </script>
</body>
</html>

JavaScript Logical Assignment operators

In JavaScript, a logical assignment operator performs a logical operation on the operands and assign the result to a variable (left operand). Each logical assignment operator is a combinations two operators, the first logical operator and second the simple assignment operator.

Following is the list of the logical assignment operators with example −

Assignment Operator Example Equivalent To
&&= (Logical AND Assignment) a &&= b a = a && b
||= (Logical OR Assignment) a ||= b a = a || b
??= (Nullish Coalescing Assignment) a ??= b a = a ?? b

Example

<html>
<body>
  <div id="output"></div>
  <script>
    var a = 5;
    var b = 10;
    var result = (a &&= b);
    document.getElementById("output").innerHTML = 
	"Value of (a &&= b) => " + result + "<br/>";
    result = (a ||= b);
    document.getElementById("output").innerHTML +=
	"Value of (a ||= b) => " + result;
  </script>
</body>
</html>
Advertisements