Table of Contents
Arithmetic operators perform arithmetic on numbers (literals or variables).
A typical arithmetic operation operates on two numbers.
The numbers (in an arithmetic operation) are called operands. The operation (to be performed between the two operands) is defined by an operator.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Arithmetic Operators Overview</h4>
<p id="demo"></p>
<script>
let x = 10;
let y = 3;
document.getElementById("demo").innerHTML =
"x = " + x + ", y = " + y + "<br><br>" +
"x + y = " + (x + y) + " (Addition)<br>" +
"x - y = " + (x - y) + " (Subtraction)<br>" +
"x * y = " + (x * y) + " (Multiplication)<br>" +
"x / y = " + (x / y) + " (Division)<br>" +
"x % y = " + (x % y) + " (Modulus)<br>" +
"x ** y = " + (x ** y) + " (Exponentiation)";
</script>
</body>
</html>

Result View Example
The addition operator (+) adds numbers:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Adding</h4>
<p id="demo"></p>
<script>
let a = 100;
let b = 50;
let x = a + b;
document.getElementById("demo").innerHTML = "a + b = " + x;
</script>
</body>
</html>

Result View Example
The subtraction operator (-) subtracts numbers:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Subtracting</h4>
<p id="demo"></p>
<script>
let a = 100;
let b = 50;
let x = a - b;
document.getElementById("demo").innerHTML = "a - b = " + x;
</script>
</body>
</html>

Result View Example
The multiplication operator (*) multiplies numbers:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Multiplying</h4>
<p id="demo"></p>
<script>
let a = 100;
let b = 50;
let x = a * b;
document.getElementById("demo").innerHTML = "a * b = " + x;
</script>
</body>
</html>

Result View Example
The division operator (/) divides numbers:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Dividing</h4>
<p id="demo"></p>
<script>
let a = 100;
let b = 50;
let x = a / b;
document.getElementById("demo").innerHTML = "a / b = " + x;
</script>
</body>
</html>

Result View Example
The modulus operator (%) returns the division remainder:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Remainder (Modulus)</h4>
<p id="demo"></p>
<script>
let a = 17;
let b = 5;
let x = a % b;
document.getElementById("demo").innerHTML = "a % b = " + x + " (remainder of " + a + " / " + b + ")";
</script>
</body>
</html>

Result View Example
The increment operator (++) increments numbers:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Incrementing</h4>
<p id="demo"></p>
<script>
let x = 5;
x++;
let y = x;
document.getElementById("demo").innerHTML = "x = 5, x++ => y = " + y;
</script>
</body>
</html>

Result View Example
The decrement operator (--) decrements numbers:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Decrementing</h4>
<p id="demo"></p>
<script>
let x = 5;
x--;
let y = x;
document.getElementById("demo").innerHTML = "x = 5, x-- => y = " + y;
</script>
</body>
</html>

Result View Example
The exponentiation operator (**) raises the first operand to the power of the second operand:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Exponentiation</h4>
<p id="demo"></p>
<script>
let x = 5;
let y = x ** 2;
document.getElementById("demo").innerHTML = "x = 5, x ** 2 = " + y;
</script>
</body>
</html>

Result View Example
Operator precedence describes the order in which operations are performed in an arithmetic expression.
Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).
Precedence can be changed by using parentheses:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arithmetic</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Operator Precedence</h4>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<script>
document.getElementById("demo1").innerHTML = "100 + 50 * 3 = " + (100 + 50 * 3);
document.getElementById("demo2").innerHTML = "(100 + 50) * 3 = " + ((100 + 50) * 3);
document.getElementById("demo3").innerHTML = "100 + 50 - 3 * 2 = " + (100 + 50 - 3 * 2);
</script>
</body>
</html>

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