JavaScript Assignment Operators Example

JavaScript Assignment Operators Example

In this tutorial, you will learn about JavaScript assignment operators. Also, 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 operatorsDescription
=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.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *