Assignment operator in javascript with example; In this tutorial, you will learn about JavaScript assignment operators with the help of examples. And as well as learn how to assign a value of the right operand to its left operand in js using these assignment operators.
List of JS Assignment Operators
Assignment operators | Description |
---|---|
= | Assigns right operand value to left operand. |
+= | Sums up left and right operand values and assign the result to the left operand. |
-= | Subtract right operand value from left operand value and assign the result to the left operand. |
*= | Multiply left and right operand values and assign the result to the left operand. |
/= | Divide left operand value by right operand value and assign the result to the left operand. |
%= | Get the modulus of left operand divide by right operand and assign resulted modulus to the left operand. |
If you want to assign a value of the right(first) operand to its left(second) operand, you can use equal operator in js.
See the example of assignment operator is equal (=):
let x = 15;
In this example, Assigned the number 15 to the variable x.
See another example for better understanding:
let x = 15; x = x + 45;
It’s equivalent to the above example:
let x = 15; x += 45;
JS assignment operator has several shortcuts for performing all the arithmetic operations, which let you assign to the right operand the left operand.
Here, you can see that examples of the following assignment operators:
+=
: addition assignment-=
: subtraction assignment*=
: multiplication assignment/=
: division assignment%=
: remainder assignment**=
: exponentiation assignment
See the following example:
let x = 50; let y = 100; output = (x = y); // 100 output = (x += y); // 200 output = (x -= y); // 100 output = (x *= y); // 1000 output = (x /= y); // 100 output = (x %= y); //0
Conclusion
In this tutorial, you have learned how to use JavaScript assignment operators.