JavaScript Arithmetic

Back to JS page

Table of Contents

Arithmetic Operators

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>

Example 1

Result View Example

Adding

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>

Example 2

Result View Example

Subtracting

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>

Example 3

Result View Example

Multiplying

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>

Example 4

Result View Example

Dividing

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>

Example 5

Result View Example

Remainder

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>

Example 6

Result View Example

Incrementing

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>

Example 7

Result View Example

Decrementing

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>

Example 8

Result View Example

Exponentiation

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>

Example 9

Result View Example

Operator Precedence

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>

Example 10

Result View Example

Document

Document in project

You can Download PDF file.

Reference