JavaScript | Assignment operators

The Assignment operator is  equal (=) which assigns the value of right-hand operand to its left-hand operand. That is if a = b assigns the value of b to a.

The simple assignment operator is used to assigning a value to a variable. The assignment operation evaluates to the assigned value. Chaining the assignment operator is possible in order to assign a single value to multiple variables. See the example.

Syntax:

data=value

Examples:

// Lets take some variables
x=10
y=20

x=y // Here, x is equal to 20
y=x // Here, y is equal to 10

There are so many assignment operator as shown in the table with the description:th



NAME SHORTHAND OPERATOR MEANING
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

Left Shift Assignment

a<<=b

a=a<<b

Right Shift Assignment

a>>=b

a=a>>b

Bitwise AND Assignment

a&=b

a=a&b



Bitwise OR Assignment

a|=b

a=a | b

Bitwise XOR Assignment

a^=b

a=a^b

Addition Assignment: This operator adds the value to the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behavior of the addition assignment operator. Addition or concatenation is possible. In case if concatenation then we use the string as an operand.

Example:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
 let a = 2;
 const b= 3;
  
 // Expected output: 2
 document.write(a);
  
 document.write('</br>');
   
 // Expected output: 4
 document.write(a = b + 1);
</script>

chevron_right


Output:

2
4

Subtraction Assignment:  This operator subtracts the value of the right operand from a variable and assigns the result to the variable.

Example:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
 let yoo=4;
  
 document.write(foo=yoo-1); // 4-1
  
</script>

chevron_right


Output:



3

Multiplication Assignment: This operator multiplies a variable by the value of the right operand and assigns the result to the variable.

Example:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
 let yoo=5;
  
 document.write(yoo=yoo*2); // 5*2
  
</script>

chevron_right


Output:

10

Division Assignment: This operator divides a variable by the value of the right operand and assigns the result to the variable.

Example:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
   let yoo=10;
   const moo=2;
  
   document.write(yoo=yoo/moo); // 10/2
  
   document.write("</br>");
  
   document.write(yoo/=0); // Infinity
  
</script>

chevron_right


Output:

5
Infinity

Remainder Assignment: This operator divides a variable by the value of the right operand and assigns the remainder to the variable.

Example:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
 let yoo=50;
  
 document.write(yoo%=10); //zero
  
</script>

chevron_right


Output:

0

Exponentiation Assignment: This operator raises the value of a variable to the power of the right operand.



Example:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
   let yoo=2;
   const moo=2;
  
   // 2 raise to the power 2
   document.write(yoo**moo);
  
</script>

chevron_right


Output:

4

Left Shift Assignment: This operator moves the specified amount of bits to the left and assigns the result to the variable.

Example:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
 var yoo=5; // 101
  
 // 20(In Binary 10100)
 document.write(yoo<<=2);
</script>

chevron_right


Output:

20

Right Shift Assignment: This operator moves the specified amount of bits to the right and assigns the result to the variable.

Example:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
 var yoo=5;
  
 document.write(yoo>>=2); // 001
  
</script>

chevron_right


Output:

1

Binary AND Assignment: This operator uses the binary representation of both operands, does a bitwise AND operation on them, and assigns the result to the variable.

Example:



Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
 var yoo=5;
  
 document.write(yoo&=2); // 000
  
</script>

chevron_right


Output:

0

Binary OR Assignment: This operator uses the binary representation of both operands, does a bitwise OR operation on them, and assigns the result to the variable.

Example:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
 var yoo=5;
  
 // 7(In binary: 111)
 document.write(yoo|=2);
  
</script>

chevron_right


Output:

7

 This operator uses the binary representation of both operands, does a bitwise XOR operation on them, and assigns the result to the variable.

Example:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
 var yoo=5;
  
 document.write(yoo^=2); // 111
  
</script>

chevron_right


Output:

7

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.