JavaScript

# Assignment Operators in JavaScript | Explained with Examples

Assignment operators are a crucial part of computer programming that are used to allocate the value of the left operand to the right operand or in simple words assign values to variables. Assignment operators perform logical operations like, bitwise logical operations or operations on integral operands or boolean operations. Javascript makes use of multiple assignment operators. Here we have listed JavaScript assignment operators for you.

## Assignment (=)

The assignment operator is used for the purpose of assigning a value to a variable.

Example:

let a=10;
console.log(a);

Output:

10

The addition assignment operator is used to add the value of the right operand to the left operand and allocates the resulting value to the variable.

Example:

let x=2;
console.log(x+=2); // or x = x + 2

Output:

4

## Subtraction Assignment (-=)

The subtraction operator subtracts the value of the right operand from the left operand and allocates the resulting value to the variable.

Example:

let a=10;
console.log(a-=2); // or a = a - 2

Output:

8

## Multiplication Assignment (*=)

The multiplication assignment multiplies the value of the right operand with the left operand and assigns the resulting value to the variable.

Example:

let x=2;
console.log(x*=2); // or x = x * 2

Output:

4

## Division Assignment (/=)

The division assignment divides the variable value by the right operand and assigns the resulting value to the variable.

Example:

let a=4;
console.log(a/=2); // or a = a / 2

Output:

2

## Remainder Assignment (%=)

The remainder operator returns the remainder that is left as a result of dividing one operand by another.

Example:

let x=3;
console.log(x%=2); // or x = x % 2

Output:

1

## Exponentiation Assignment (**=)

Exponentiation operator is used to raise the value of the variable to the right operand.

Example:

let x=3;
console.log(x**=2); // or x = x ** 2

Output:

9

## Left Shift Assignment (<<=)

The left shift operator pushes a particular number of bits to the left and the resulting value is assigned to the variable.

Example:

let a = 5; // 00000000000000000000000000000101
a<<=2;  // 00000000000000000000000000010100
console.log(a);

Output:

20

## Right Shift Assignment (>>=)

The right shift operator pushes a particular number of bits to the right and the resulting value is assigned to the variable.

Example:

let y = 5; // 00000000000000000000000000000101
y>>=2;  // 00000000000000000000000000010100
console.log(y)

Output:

1

## Unsigned Right Shift Assignment (>>>=)

The right shift operator pushes a particular number of bits to the right and the resulting value is assigned to the variable. Positive numbers are shifted to the right with same effect as the right shift operator, meanwhile, for negative numbers empty bits are replaced by zeros.

Example:

let x = 5; // 00000000000000000000000000000101
x>>>=2;  // 00000000000000000000000000010100
console.log(x)

Output:

1

## Bitwise AND Assignment (&=)

This operator uses the binary codes of the both left and right operand and performs AND function then assigns the result to the variable.

Example:

let x = 5; // 00000000000000000000000000000101
x &=3;  // 00000000000000000000000000000011
console.log(x);

Output:

1 // 00000000000000000000000000000001

## Bitwise XOR Assignment (^=)

This operator uses the binary codes of the both left and right operand to perform XOR and assigns the result to the variable.

Example:

let a = 5; // 00000000000000000000000000000101
a ^=3;  // 00000000000000000000000000000011
console.log(x);

Output:

6 // 00000000000000000000000000000110

## Bitwise OR Assignment (|=)

This operator uses the binary codes of the both left and right operand to perform OR and assigns the result to the variable.

Example:

let a = 5; // 00000000000000000000000000000101
a |=3;  // 00000000000000000000000000000011
console.log(x);

Output:

7 // 00000000000000000000000000000111

## Conclusion

The assignment operators in JavaScript are useful when assigning values to the operands or performing different arithmetic operations on the variables in an expression. There are numerous assignment operators in JavaScript which are used for various purposes. This tutorial highlights these operators along with their examples.