The Wayback Machine - https://web.archive.org/web/20230322220241/https://www.geeksforgeeks.org/javascript-assignment-operators/
Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript Assignment Operators

Improve Article
Save Article
  • Last Updated : 28 Feb, 2023
Improve Article
Save Article

JavaScript assignment operator is equal (=) which assigns the value of the 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 assign a value to a variable. The assignment operation evaluates 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

Example:

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

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

 

 

Assigment Operators List: There are so many assignment operators as shown in the table with the description.

OPERATOR NAMESHORTHAND OPERATORMEANING
Addition Assignmenta+=b

a=a+b

Subtraction Assignmenta-=b

a=a-b

Multiplication Assignmenta*=b

a=a*b

Division Assignmenta/=b

a=a/b

Remainder Assignmenta%=b

a=a%b

Exponentiation Assignmenta**=b

a=a**b

Left Shift Assignmenta<<=b

a=a<<b

Right Shift Assignmenta>>=b

a=a>>b

Bitwise AND Assignmenta&=b

a=a&b;

Bitwise OR Assignmenta|=b

a=a | b

Bitwise XOR Assignmenta^=b

a=a^b

Bellow we have described each operators with a example code

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 of concatenation then we use the string as an operand.

Example:

Javascript




<script>
    let a = 2;
    const b= 3;
       
    // Expected output: 2
    console.log(a);
       
    // Expected output: 4
    console.log(a = b + 1);
</script>

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




<script>
    let yoo=4;
      
    // Expected output 3
    console.log(foo=yoo-1); 
</script>

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




<script>
    let yoo=5;
      
    // Expected output 10
    console.log(yoo=yoo*2); 
</script>

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




<script>
    let yoo=10;
    const moo=2;
      
    // Expected output 5
    console.log(yoo=yoo/moo);
      
    // Expected output Infinity    
    console.log(yoo/=0);
</script>

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




<script>
    let yoo=50;
      
    // Expected output 0
    console.log(yoo%=10);
</script>

Output:

0

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

Example:

Javascript




<script>
    let yoo=2;
    const moo=2;
      
    // Expected output 4
    console.log(yoo**moo);
</script>

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




<script>
    var yoo=5;
      
    // Expected output 20(In Binary 10100)
    console.log(yoo<<=2);
</script>

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




<script>
    var yoo=5;
      
    // Expected Output 1(In binary 001)
    console.log(yoo>>=2); 
</script>

Output:

1

Bitwise 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




<script>
    var yoo=5;
      
    // Expected output 0(In binary 000)
    console.log(yoo&=2);
</script>

Output:

0

Bitwise 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




<script>
    var yoo=5;
      
    // Expected output 7(In binary 111)
    console.log(yoo|=2);
</script>

Output:

7

Bitwise XOR Assignment: 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




<script>
    var yoo=5;
      
    // Expected output 7(In binary 111)
    console.log(yoo^=2);
</script>

Output:

7

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!