Assignment operators in JavaScript are used to assign values to variables and update them quickly. They are essential for storing and manipulating data efficiently within your scripts.
JavaScript supports the following assignment operators:
| Operator | Name | Description | Example | Equivalent |
|---|---|---|---|---|
= |
Simple Assignment | Assigns values from the right side operand to the left side operand. | C = A + B |
Assigns the value of A + B into C |
+= |
Add and Assignment | Adds the right operand to the left operand and assigns the result to the left operand. | C += A |
C = C + A |
-= |
Subtract and Assignment | Subtracts the right operand from the left operand and assigns the result to the left operand. | C -= A |
C = C - A |
*= |
Multiply and Assignment | Multiplies the right operand with the left operand and assigns the result to the left operand. | C *= A |
C = C * A |
/= |
Divide and Assignment | Divides the left operand with the right operand and assigns the result to the left operand. | C /= A |
C = C / A |
%= |
Modulus and Assignment | Takes modulus using two operands and assigns the result to the left operand. | C %= A |
C = C % A |
Note: The same logic applies to Bitwise operators, so they will become <<=, >>=, >>>=, &=, |=, and ^=.
Try the following code to see how assignment operators work in JavaScript.
var a = 33; var b = 10;console.log("Value of a => (a = b) => " + (a = b)); console.log("Value of a => (a += b) => " + (a += b)); console.log("Value of a => (a -= b) => " + (a -= b)); console.log("Value of a => (a *= b) => " + (a *= b)); console.log("Value of a => (a /= b) => " + (a /= b)); console.log("Value of a => (a %= b) => " + (a %= b));
What is the equivalent of the expression x += y in JavaScript?