JavaScript Arithmetic Operators - Java Operators with Example



JavaScript Arithmetic Operators

  • Arithmetic operators perform arithmetic on numbers (literals or variables).
Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation (ES2016)
/ Division
% Modulus (Remainder)
++ Increment
-- Decrement

Arithmetic Operations

  • A typical arithmetic operation operates on two numbers.
  • The two numbers can be literals.

Sample Code

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arithmetic</h2>
<p>A typical arithmetic operation takes two numbers and produces a new number.</p>

<p id="demo"></p>

<script>
let x = 100 + 50;
document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

Output

JavaScript Arithmetic
A typical arithmetic operation takes two numbers and produces a new number.
150

Operators and Operands

  • The numbers (in an arithmetic operation) are called operands.
  • The operation (to be performed between the two operands) is defined by an operator.
Operand Operator Operand
100 + 50

Adding

  • The addition operator (+) adds numbers:
javascript-add-two-numbers

Sample Code

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Arithmetic</h1>
<h2>The + Operator</h2>

<p id="demo"></p>

<script>
let a = 5;
let b = 5;
let c = a + b;
document.getElementById("demo").innerHTML = c;
</script>

</body>
</html>

Output

JavaScript Arithmetic
The + Operator
10

Subtracting

  • The subtraction operator (-) subtracts numbers.
javascript-sum-two-numbers

Sample Code

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arithmetic</h1>
<h2>The - Operator</h2>
<p id="demo"></p>
<script>
let a = 10;
let b = 5;
let c = a - b;
document.getElementById("demo").innerHTML = c;
</script>
</body>
</html>

Output

JavaScript Arithmetic
The - Operator
5

Multiplying

  • The multiplication operator (*) multiplies numbers.
javascript-multiply-two-numbers

Sample Code

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Arithmetic</h1>
<h2>The * Operator</h2>

<p id="demo"></p>

<script>
let a = 5;
let b = 5;
let c = a * b;
document.getElementById("demo").innerHTML = c;
</script>

</body>
</html>

Output

JavaScript Arithmetic
The * Operator
25

Dividing

  • The division operator (/) divides numbers.
javascript-divide-two-numbers

Sample Code

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Arithmetic</h1>
<h2>The / Operator</h2>

<p id="demo"></p>

<script>
let x = 5;
let y = 2;
let z = x / y;
document.getElementById("demo").innerHTML = z;
</script>

</body>
</html>

Output

JavaScript Arithmetic
The / Operator
2.5

Remainder

  • The modulus operator (%) returns the division remainder.
javascript-division-two-numbers

Sample Code

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Arithmetic</h1>
<h2>The % Operator</h2>

<p id="demo"></p>

<script>
let x = 5;
let y = 10;
let z = x % y;
document.getElementById("demo").innerHTML = z;
</script>

</body>
</html>

Output

JavaScript Arithmetic
The % Operator
5

Incrementing

  • The increment operator (++) increments numbers.
javascript-increment-counter

Sample Code

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Arithmetic</h1>
<h2>The ++ Operator</h2>

<p id="demo"></p>

<script>
let x = 5;
x++;
let z = x;
document.getElementById("demo").innerHTML = z;
</script>

</body>
</html>

Output

JavaScript Arithmetic
The ++ Operator
6

Decrementing

  • The decrement operator (--) decrements numbers.

Sample Code

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Arithmetic</h1>
<h2>The -- Operator</h2>

<p id="demo"></p>

<script>
let x = 5;
x--;
let z = x;
document.getElementById("demo").innerHTML = z;
</script>

</body>
</html>

Output

JavaScript Arithmetic
The -- Operator
4

Exponentiation

  • The exponentiation operator (**) raises the first operand to the power of the second operand.

Sample Code

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Arithmetic</h1>
<h2>The ** Operator</h2>

<p id="demo"></p>

<script>
let x = 5;
document.getElementById("demo").innerHTML = x ** 2;
</script>

</body>
</html>

Output

JavaScript Arithmetic
The ** Operator
25

Operator Precedence

  • Operator precedence describes the order in which operations are performed in an arithmetic expression.

Related Searches to JavaScript Arithmetic Operators - Java Operators with Example