Assignment operators are used to assign values to variables.
Operator Example Operation = Assign Equals to x = y += Addition Equals to x = x + y -= Subtraction Equals to x = x - y *= Multiplication Equals to x = x * y /= Division Equals to x = x / y %= Modulus Equals to x = x % y
The equal (=) operator assigns a value to a variable.
<!DOCTYPE html>
<html>
<body>
<h1>The = Operator</h1>
<p id="demo"></p>
<script>
var x = 10;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
The += operator adds value to variable. In below example, 5 is added to value of x. So it becomes 15.
<!DOCTYPE html>
<html>
<body>
<h1>The += Operator</h1>
<p id="demo"></p>
<script>
var x = 10;
x += 5;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
The -= operator subtracts value from variable. In below example, 5 is subtracted from value of x. So it becomes 5.
<!DOCTYPE html>
<html>
<body>
<h1>The -= Operator</h1>
<p id="demo"></p>
<script>
var x = 10;
x -= 5;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
The *= operator multiplies value of a variable. In below example, value of x is multiplied by 5. So it becomes 50.
<!DOCTYPE html>
<html>
<body>
<h1>The -= Operator</h1>
<p id="demo"></p>
<script>
var x = 10;
x *= 5;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
The /= operator divides a variable value. In below example, value of x is divided by 5. So it becomes 2.
<!DOCTYPE html>
<html>
<body>
<h1>The -= Operator</h1>
<p id="demo"></p>
<script>
var x = 10;
x /= 5;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
The %= operator divides a variable value and returns reminder. In below example, value of x is divided by 5 and its reminder is assigned to x.
<!DOCTYPE html>
<html>
<body>
<h1>The -= Operator</h1>
<p id="demo"></p>
<script>
var x = 10;
x %= 5;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>