JavaScript Assignment Operators
The JavaScript assignment operator assigns the value of the operand on the right-side to the operand on the left-side. The left-side operand could be a variable, property or an array. The assignment operator has right to left associativity.
Following is a list of assignment operators available in Javascript.
| Operator | Description | Example |
|---|---|---|
| = | Assignment | a += b is equivalent to a = a + b |
| += | Addition assignment | a += b is equivalent to a = a + b |
| -= | Subtraction assignment | a -= b is equivalent to a = a - b |
| *= | Multiplication assignment | a *= b is equivalent to a = a * b |
| /= | Division assignment | a /= b is equivalent to a = a / b |
| %= | Remainder assignment | a %= b is equivalent to a = a % b |
| **= | Exponentiation assignment | a **= b is equivalent to a = a ** b |
| <<= | Left shift assignment | a <<= b is equivalent to a = a << b |
| >>= | Right shift assignment | a >>= b is equivalent to a = a >> b |
| >>>= | Unsigned right shift assignment | a >>>= b is equivalent to a = a >>> b |
| &= | Bitwise AND assignment | a &= b is equivalent to a = a & b |
| |= | Bitwise OR assignment | a |= b is equivalent to a = a | b |
| ^= | Bitwise XOR assignment | a ^= b is equivalent to a = a ^ b |
| &&= | Logical AND assignment | x = x && (x = y) |
| ||= | Logical OR assignment | x = x || (x = y) |
| ??= | Nullish coalescing assignment | x = x ?? (x = y) |
You already learned about = assignment operator, which assigns value of its right operand to the left operand.
// Assigns value 5 to variable x
var x = 5;
console.log(x); // 5
Chaining JavaScript assignment operator
Chaining assignment operator helps when you want to assign same value to multiple variables.
var a;
var b;
var c = 10;
a = b = c; // value of all variables is 10
console.log(a); // 10
console.log(b); // 10
console.log(c); // 10
As the assignment operator has right to left assiciativity. In the above example, first value of c, 10 is assigned to b and then value of b, 10 is assigned to a. In this way value of all variables becomes same i.e.,10.
The Addition Assignment Operator ( += )
The addition assignment operator adds the value of right-side operand and assign to left-side operand.
var a =2;
a+=1;
console.log(a); // 3
The subtraction assignment operator ( -= )
The subtraction assignment operator subtracts the value of the right-side operand and assign it to left-side operand.
var a =2;
a-=1;
console.log(a); // 1
The Multiplication Assignment Operator ( *= )
The multiplication assignment operator multiplies the value of the right-side operand and assign it to the left-side operand.
var a =2;
a*=5;
console.log(a); // 10
The Division Assignment Operator ( /= )
The division assignment operator divides the value of the right-side operand and assign it to the left-side operand.
var a =10;
a/=2;
console.log(a); // 5
The Remainder Assignment Operator ( %= )
The remainder assignment operator evaluates modulus of the right-side operand and assign it to the left-side operand.
var a =17;
a%=5;
console.log(a); // 2