JAVASCRIPT ASSIGNMENT OPERATORS
The assignment operators are used to assign value based on the right operand to its left operand. The left operand must be a variable while the right operand may be a variable, number, boolean, string, expression, object or combination of any other.
One of the most basic assignment operators is equal (=), which is used to directly assign a value.
Assignment Operators List
Here is the list of all assignment operators in JavaScript:
Operator name | Operator | Meaning |
---|---|---|
Assignment operator | a = 10 | |
Addition assignment operator | a += b | a = a + b |
Subtraction assignment operator | a -= b | a = a - b |
Multiplication assignment operator | a *= b | a = a * b |
Division assignment operator | a /= b | a = a / b |
Remainder assignment operator | a %= b | a = a % b |
Exponentiation assignment operator | a **= b | a = a ** b |
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 |
Bitwise AND assignment | a &= b | a = a & b |
Bitwise OR assignment | a |= b | a = a | b |
Bitwise XOR assignment | a ^= b | a = a ^ b |
Logical AND assignment | a &&= b | a = a && b |
Logical OR assignment | a ||= b | a = a || b |
Logical nullish assignment | a ??= b | a = a ?? b |
Assignment operator
The assignment operator (=) is the simplest value assigning operator which assigns a given value to a variable.
The assignment operators support chaining, which means you can assign a single value in multiple variables in a single line.
Addition assignment operator
The addition assignment operator (+=) is used to add the value of right operand to the value of left operand and assigns the value to the left operand.
On the basic of data type of variable the addition assignment operator may add or concatenate the variables.
Subtraction assignment operator
The subtraction assignment operator (-=) subtracts the value of right operand from the value of left operand and assigns the value to left operand.
If value can not be subtracted then it results to a NaN
.
Multiplication assignment operator
The multiplication assignment operator (*=) assigns a value to left operand by multiplying value of left and right operand.
Division assignment operator
The division assignment operator (/=) divides the value of left operand by the value of right operand and assigns the result to the left operand.
Remainder assignment operator
The remainder assignment operator (%=) assigns the remainder value when the value of left operand is divided by the value of right operand.
Exponentiation assignment operator
The exponential assignment operator (**=) rises the value of left operator to the power of right operand and assigns the result to the left operand.
Left shift assignment
The left shift assignment operator (<<=) moves the bit value of left operand by the value of right operand in left direction and assigns the value to left operand.
Right shift assignment
The right shift assignment operator (>>=) moves the bit value of the left operand in right direction by the value of right operand and assigns the result to left operand.
Unsigned right shift assignment
The unsigned right shift assignment operator >>>= moves the bit value of left operand in right direction by the value of right operand and results positive output.
Bitwise AND assignment
The bitwise AND assignment operator (&=) does a bitwise AND operation on the bit value of both the operands and store it to the left operand.
Bitwise OR assignment
The bitwise OR assignment operator (|=) does a bitwise OR operation on the bit value of both the operands and store it to the left operand.
Bitwise XOR assignment
The bitwise XOR assignment operator (^=) does a bitwise XOR operation on the bit value of both the operands and store it to the left operand.
Logical AND assignment
The logical AND assignment operator (&&=) assigns value to left operand only when it is truthy.
Note: A truthy value is a value which is considered true
when encountered in boolean context.
Logical OR assignment
The logical OR assignment operator (||=) assigns value to left operand only when it is falsy.
Note: A falsy value is a value which is considered false
when encountered in boolean context.
Logical nullish assignment
The logical nulish assignment operator (??=) assigns value to left operand only when it is nulish (null
or undefined
).