Java Script


JavaScript Tutorial


Admission Enquiry Form

Assignment Operators in JavaScript?

An assignment operator is the operator that assigns the value from its right side to its left side or we can say an assignment operator is used to assign a value to a variable. The most common assignment operator is =.
For example, num = 8; Here, it assigning a value of '8' to the variable 'num'.

Some other examples of assignment operators

OPERATOR EXAMPLE SAME AS
=

x=y

x=y

+=

x+=y

x=x+y
-=

x-=y

x=x-y
/=

x/=y

x=x/y
*=

x*=y

x=x*y
%=

x%=y

x=x%y

Example of Assignment operators

<html>
<head>
<title>Example of assignment operator</title>
</head>
<body>
<script>
var num=10,res;
res=num;
document.write("res = "+res+"<br>");
res+=num;
document.write("res = "+res+"<br>");
res-=num;
document.write("res = "+res+"<br>");
res*=num;
document.write("res = "+res+"<br>");
res/=num;
document.write("res = "+res+"<br>");
res%=num;
document.write("res = "+res);
</script>
</body>
</html>

Output:

res = 10
res = 20
res = 10
res = 100
res = 10
res = 0