Table of Contents
Assignment operators assign values to JavaScript variables.
Given that x = 10 and y = 5, the table below explains the assignment operators:
| Operator | Example | Same As | Result |
|---|---|---|---|
= |
x = y |
x = y |
x = 5 |
+= |
x += y |
x = x + y |
x = 15 |
-= |
x -= y |
x = x - y |
x = 5 |
*= |
x *= y |
x = x * y |
x = 50 |
/= |
x /= y |
x = x / y |
x = 2 |
%= |
x %= y |
x = x % y |
x = 0 |
**= |
x **= y |
x = x ** y |
x = 100000 |
The Simple Assignment Operator assigns a simple value to a variable:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Assignment</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The = Operator</h4>
<p id="demo"></p>
<script>
let x = 10;
document.getElementById("demo").innerHTML = "x = " + x;
</script>
</body>
</html>

Result View Example
The Addition Assignment Operator adds a value to a variable:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Assignment</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The += Operator</h4>
<p id="demo"></p>
<script>
let x = 10;
x += 5;
document.getElementById("demo").innerHTML = "x += 5 => " + x;
</script>
</body>
</html>

Result View Example
The Subtraction Assignment Operator subtracts a value from a variable:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Assignment</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The -= Operator</h4>
<p id="demo"></p>
<script>
let x = 10;
x -= 5;
document.getElementById("demo").innerHTML = "x -= 5 => " + x;
</script>
</body>
</html>

Result View Example
The Multiplication Assignment Operator multiplies a variable:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Assignment</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The *= Operator</h4>
<p id="demo"></p>
<script>
let x = 10;
x *= 5;
document.getElementById("demo").innerHTML = "x *= 5 => " + x;
</script>
</body>
</html>

Result View Example
The Exponentiation Assignment Operator raises a variable to the power of the operand:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Assignment</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The **= Operator</h4>
<p id="demo"></p>
<script>
let x = 10;
x **= 5;
document.getElementById("demo").innerHTML = "x **= 5 => " + x;
</script>
</body>
</html>

Result View Example
The Division Assignment Operator divides a variable:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Assignment</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The /= Operator</h4>
<p id="demo"></p>
<script>
let x = 10;
x /= 5;
document.getElementById("demo").innerHTML = "x /= 5 => " + x;
</script>
</body>
</html>

Result View Example
The Remainder Assignment Operator assigns a remainder to a variable:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Assignment</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The %= Operator</h4>
<p id="demo"></p>
<script>
let x = 10;
x %= 5;
document.getElementById("demo").innerHTML = "x %= 5 => " + x;
</script>
</body>
</html>

Result View Example
Two assignment operators can assign values to strings:
The Simple Assignment Operator (=) assigns a simple value to a string.
The Addition Assignment Operator (+=) adds (concatenates) strings:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Assignment</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>String Assignment</h4>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
let text1 = "Hello";
let text2 = "World";
text2 += "!";
document.getElementById("demo1").innerHTML = "text1 = " + text1;
document.getElementById("demo2").innerHTML = "text2 += \"!\" => " + text2;
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.