JavaScript Assignment

Back to JS page

Table of Contents

JavaScript Assignment Operators

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 = Operator

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>

Example 1

Result View Example

The += Operator

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>

Example 2

Result View Example

The -= Operator

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>

Example 3

Result View Example

The *= Operator

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>

Example 4

Result View Example

The **= Operator

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>

Example 5

Result View Example

The /= Operator

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>

Example 6

Result View Example

The %= Operator

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>

Example 7

Result View Example

String Assignment

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>

Example 8

Result View Example

Document

Document in project

You can Download PDF file.

Reference