Learn JavaScript

JavaScript Assignment

JavaScript assignment operators are generally used to assign the values to a variable. It includes the operators like {=, +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, ^=, |=, **=}.

ShorthandExpressionDescription
a = ba = bAssigns the value of b to variable a.
a+=ba = a + bAdds a and b, then assign the final value to variable a.
a-=ba = a – bsubtract b from a, then assigns the final value to a.
a*=ba = a*bmultiply a and b, then assigns the final value to a.
a/=ba = a/bdivides a by b, then assigns the the quotient to a.
a%=ba = a%bdivides a by b, then assigns the the remainder to a.
a**=ba = a**braise the value of a to the power of b, then assigns the final value to a.
a<<=ba = a<<bperforms the left shift, then assigns the final value to a.
a>>=ba = a>>bperforms the sign propagating right shift, then assigns the final value to a.
a>>>=ba = a>>>bperforms a zero fill right shift, then assigns the final value to a.
a&=ba = a & bperforms the bit wise AND, then assigns the final value to a.
a^=ba = a ^ bperforms the bit wise XOR, then assigns the final value to a.
a|=ba = a | bperforms the bit wise OR, then assigns the final value to a.

Example

<html>
<body>
<h2> JavaScript Program to Understand Assignment Operators</h2>
<script>
var a = 2, b = 6;
var assign, add, sub, mul, div, rem;
assign = (a = b);
add = (a+=b);
sub = (a-=b);
mul = (a*=b);
div = (a/=b);
rem = (a%=b);
document.write("Value of a After Assignment is " + assign + "<br />"); // value of a becomes 6
document.write("Value of a After execution of add assignment is " + add + "<br />"); // value of a becomes 12
document.write("Value of a After execution of subtract assignment is " + sub + "<br />"); // value of a becomes 6
document.write("Value of a After execution of multiplication assignment is " + mul + "<br />"); // value of a becomes 36
document.write("Value of a After execution of division assignment is " + div + "<br />"); // value of a becomes 6
document.write("Value of a After execution of remainder assignment is " + rem + "<br />"); // value of a becomes 0
</script>
</body>
</html>

Output

JavaScript Program to Understand Assignment Operators

Value of a After Assignment is 6
Value of a After execution of add assignment is 12
Value of a After execution of subtract assignment is 6
Value of a After execution of multiplication assignment is 36
Value of a After execution of division assignment is 6
Value of a After execution of remainder assignment is 0

Audience

This JavaScript tutorial is designed to help the beginners as well as the professional developers. For better understanding the language of the tutorial is kept simple and easy to understand.

Prerequisites

This tutorial demands to have the basic knowledge of the programming languages such as c or c++, HTML for the better understanding of the JavaScript programming language. The other basic requirement to learn JavaScript through this tutorial is the basic knowledge of computer and information technology so you can easily acquire a better understanding JavaScript programming.

Community Helping

In case you find any kind of error like typing error, spelling mistakes, incorrect grammar, incorrect information or any other mistakes. Please feel free to write to us at: akyadav@etutororg.com.