JavaScript Assignment Operators Example

JavaScript Assignment Operators Example

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 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.

Recommended JavaScript Tutorial

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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