Assignment Operators In Java Script

As the name suggest these operators are used to assign values to variables. The right hand value or variable is assigned to the left hand variable. Different assignment operators are listed below with these use.
 
Operator Description Example
= This operator is used to assign value of variable/value only at right hand of it to the variable at left hand of this operator. a=5 b=6 c=a+b
+= Increments the left hand variable with the right hand variable a+=b or a=a+b
-= Decrements the left hand variable with the right hand variable a-=b or a=a-b
*= Multiplies the left hand variable with the right hand variable a*=b or a=a*b
/= Divides the left hand variable with the right hand variable a/=b or a=a/b
%= Keeps the remainder of division of left hand variable by right hand variable in the left hand variable a%=b or a=a%b
 
 
Example
 
<html>

<head>

</head>

<body>

<script type=”text/javascript”>

var a,b

a=5

b = 6

document.write(“<br>Value of a : “+a);

document.write(“<br>Value of b : “+b);

a+=b;

document.write(“<br>Value of a after a+=b : “+a);

a-=b

document.write(“<br>Value of a after a-=b : “+a);

a*=b

document.write(“<br>Value of a after a*=b : “+a);

a/=b

document.write(“<br>Value of a after a/=b : “+a);

a%=b

document.write(“<br>Value of a after a%=b : “+a);

</script>

</body>

</html>

 
 
Output:
 
Value of a : 5
Value of b : 6
Value of a after a+=b : 11
Value of a after a-=b : 5
Value of a after a*=b : 30
Value of a after a/=b : 5
Value of a after a%=b : 5
Scroll to Top