JavaScript Arithmetic Operators: Learn with Examples

JavaScript is one of the most popular programming languages used to create interactive websites and web applications. Whether you are developing a calculator, building an online store, or creating a simple web-based tool, mathematical calculations are often an important part of the process.

This is where JavaScript arithmetic operators become useful.

Arithmetic operators allow you to perform mathematical operations such as addition, subtraction, multiplication, and division. They also help you calculate remainders, work with powers, and increase or decrease numeric values.

If you are just starting with JavaScript, understanding these operators will give you a strong foundation for writing useful and practical code.

In this tutorial, we will explore the different arithmetic operators in JavaScript, understand how they work, and practise using them through simple examples. By the end, you will have a clearer understanding of how JavaScript handles mathematical calculations.

Watch the JavaScript Arithmetic Operators Video

Before getting into the examples, watch the short video below to see the topic in action.

Watch: JavaScript Arithmetic Operators

You can use the video as a quick introduction and follow the explanations in this article to understand each operator in more detail.

What Are Arithmetic Operators in JavaScript?

Arithmetic operators are special symbols that tell JavaScript to perform mathematical calculations.

You already use many of these symbols in everyday mathematics. For example, the plus sign is used for addition, the minus sign is used for subtraction, and the division symbol is used to divide numbers.

JavaScript provides similar operators, but some symbols are written differently when working with code.

For example, the multiplication symbol is represented by an asterisk (*), while division uses a forward slash (/).

Let’s look at a simple example:

let number1 = 20;
let number2 = 10;

let result = number1 + number2;

console.log(result);

Output:

30

In this example, we created two variables and used the addition operator to calculate their total.

The result is stored in a third variable and displayed in the console.

This is the basic idea behind arithmetic operators: they allow JavaScript to work with numeric values and produce calculated results.

Different Types of Arithmetic Operators in JavaScript

JavaScript supports several arithmetic operators, and each one has a specific purpose.

Here is a quick overview of the most commonly used operators:

OperatorNameExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52
%Remainder (modulus)10 % 31
**Exponentiation2 ** 38
++Incrementnumber++Increases by 1
--Decrementnumber--Decreases by 1

Let’s explore each operator with examples.

1. Addition Operator (+)

The addition operator (+) is used to add two or more numbers together.

It is one of the simplest and most frequently used operators in JavaScript.

Example:

let a = 15;
let b = 25;

let total = a + b;

console.log(total);

Output:

40

JavaScript adds the values of a and b, then stores the result in the total variable.

You can also add several numbers in one expression:

let result = 10 + 20 + 30 + 40;

console.log(result);

Output:

100

Practical Example: Calculate a Shopping Total

Imagine you are building an online store where a customer purchases two products.

You can use the addition operator to calculate the total cost.

let product1 = 50;
let product2 = 30;

let totalPrice = product1 + product2;

console.log(totalPrice);

Output:

80

This is a simple example of how arithmetic operators can be used in real-world web applications.

Remember: The plus operator can also join strings together. For example, "Hello" + " World" produces "Hello World". This is called string concatenation.

2. Subtraction Operator (-)

The subtraction operator (-) is used to subtract one number from another.

It is useful when calculating differences, remaining balances, discounts, or other values where one amount needs to be deducted from another.

Example:

let a = 100;
let b = 35;

let result = a - b;

console.log(result);

Output:

65

Here, JavaScript subtracts 35 from 100 and returns 65.

Practical Example: Calculate a Discounted Price

Suppose a product costs $120 and the store offers a $20 discount.

You can calculate the final price using subtraction:

let originalPrice = 120;
let discount = 20;

let finalPrice = originalPrice - discount;

console.log(finalPrice);

Output:

100

The customer pays $100 after the discount is deducted.

3. Multiplication Operator (*)

The multiplication operator (*) allows you to multiply two or more numbers.

Unlike traditional mathematics, JavaScript does not use the × symbol for multiplication. Instead, it uses an asterisk.

Example:

let a = 12;
let b = 4;

let result = a * b;

console.log(result);

Output:

48

JavaScript multiplies 12 by 4 and returns 48.

Practical Example: Calculate Product Quantity

Imagine a customer wants to purchase five items, and each item costs $10.

You can calculate the total price by multiplying the price by the quantity.

let price = 10;
let quantity = 5;

let total = price * quantity;

console.log(total);

Output:

50

This calculation is commonly used in shopping carts, invoices, and product pricing systems.

4. Division Operator (/)

The division operator (/) is used to divide one number by another.

It can help you calculate averages, split amounts equally, and determine the value of individual items.

Example:

let a = 100;
let b = 4;

let result = a / b;

console.log(result);

Output:

25

JavaScript divides 100 by 4 and returns 25.

Practical Example: Calculate an Average

Suppose a student receives the following marks in three subjects:

  • Mathematics: 80
  • English: 90
  • Science: 70

You can calculate the average using addition and division.

let math = 80;
let english = 90;
let science = 70;

let average = (math + english + science) / 3;

console.log(average);

Output:

80

The parentheses make sure all three marks are added before the total is divided by three.

What Happens When You Divide by Zero?

JavaScript handles division by zero differently from some programming languages.

For example:

console.log(10 / 0);
console.log(0 / 0);

Output:

Infinity
NaN

A nonzero number divided by zero produces Infinity, while zero divided by zero produces NaN, which means “Not a Number.”

When building a calculator or other application, you should check that the divisor is not zero before performing a division.

5. Modulus Operator (%)

The modulus operator (%) returns the remainder after dividing one number by another.

It is especially useful when checking whether a number is even or odd, dividing items into groups, or working with repeating patterns.

Example:

let a = 17;
let b = 5;

let result = a % b;

console.log(result);

Output:

2

Why is the result 2?

Because 17 divided by 5 gives a quotient of 3, with 2 remaining.

Practical Example: Check Whether a Number Is Even or Odd

One of the most common uses of the modulus operator is checking whether a number is even or odd.

let number = 12;

if (number % 2 === 0) {
    console.log("This is an even number.");
} else {
    console.log("This is an odd number.");
}

Output:

This is an even number.

If a number divided by 2 leaves a remainder of zero, it is even. Otherwise, it is odd.

This is a great example of combining arithmetic operators with JavaScript conditional statements.

6. Exponentiation Operator (**)

The exponentiation operator (**) is used to raise a number to a particular power.

For example, if you want to calculate 2 to the power of 3, JavaScript allows you to write 2 ** 3.

Example:

let number = 2;
let power = 3;

let result = number ** power;

console.log(result);

Output:

8

This calculation means:

2 × 2 × 2 = 8.

Here is another example:

console.log(5 ** 2);

Output:

25

The exponentiation operator is useful for mathematical calculations, scientific applications, and projects involving powers.

7. Increment Operator (++)

The increment operator (++) increases a numeric variable by one.

It is commonly used in counters, loops, and situations where a value needs to increase gradually.

Example:

let number = 5;

number++;

console.log(number);

Output:

6

The original value is 5. After applying the increment operator, it becomes 6.

Prefix and Postfix Increment

There are two ways to use the increment operator: prefix and postfix.

Postfix increment:

let number = 5;

let result = number++;

console.log(result);
console.log(number);

Output:

5
6

With postfix increment, JavaScript uses the original value in the expression first and then increases the variable.

Prefix increment:

let number = 5;

let result = ++number;

console.log(result);
console.log(number);

Output:

6
6

With prefix increment, JavaScript increases the value first and then uses the updated value in the expression.

Understanding this difference is helpful when working with counters and loops.

8. Decrement Operator (–)

The decrement operator (--) performs the opposite operation of increment.

It decreases a numeric variable by one.

Example:

let number = 10;

number--;

console.log(number);

Output:

9

The variable originally contains 10. After using the decrement operator, its value becomes 9.

Prefix and Postfix Decrement

Like increment, decrement also has prefix and postfix forms.

Postfix decrement:

let number = 10;

let result = number--;

console.log(result);
console.log(number);

Output:

10
9

Prefix decrement:

let number = 10;

let result = --number;

console.log(result);
console.log(number);

Output:

9
9

These operators are useful for countdowns, loops, and other applications where a number needs to decrease step by step.

Understanding Arithmetic Operator Precedence

When you use multiple arithmetic operators in one expression, JavaScript follows a specific order of operations.

This is known as operator precedence.

For example:

let result = 10 + 5 * 2;

console.log(result);

Output:

20

Why does JavaScript return 20 instead of 30?

Because multiplication has higher precedence than addition.

JavaScript performs the multiplication first:

5 × 2 = 10

Then it performs the addition:

10 + 10 = 20

You can change the order of calculations by using parentheses.

let result = (10 + 5) * 2;

console.log(result);

Output:

30

This time, JavaScript calculates the expression inside the parentheses first.

Using parentheses is a good habit because it makes your calculations easier to understand and reduces confusion.

Common Mistakes When Using Arithmetic Operators

Even though arithmetic operators are simple, beginners sometimes make mistakes when using them.

Here are a few common examples worth understanding.

1. Using the Wrong Multiplication Symbol

JavaScript uses the asterisk (*) for multiplication, not the mathematical multiplication symbol (×).

Incorrect:

let result = 5 × 4;

Correct:

let result = 5 * 4;

Always use the asterisk when multiplying numbers in JavaScript.

2. Confusing Division With Modulus

The division operator (/) returns the quotient, while the modulus operator (%) returns the remainder.

For example:

console.log(10 / 3);
console.log(10 % 3);

Output:

3.3333333333333335
1

Both operators are useful, but they perform different calculations.

3. Forgetting Operator Precedence

When multiple operators are used together, JavaScript does not always calculate from left to right.

For example:

let result = 10 + 5 * 2;

The result is 20 because multiplication happens before addition.

Use parentheses when you want a different order or want to make the expression clearer.

4. Expecting Perfect Decimal Results

JavaScript uses floating-point numbers for ordinary numeric calculations. Some decimal values cannot be represented exactly in binary.

For example:

let result = 0.1 + 0.2;

console.log(result);

Output:

0.30000000000000004

This is a normal floating-point precision issue, not a mistake in your code.

For displaying a result with two decimal places, you can use:

let result = 0.1 + 0.2;

console.log(result.toFixed(2));

Output:

0.30

Keep in mind that toFixed() returns a string. For applications that perform financial calculations, consider using integer-based arithmetic or a suitable decimal library.

Frequently Asked Questions

1. What are arithmetic operators in JavaScript?

Arithmetic operators are symbols used to perform mathematical operations, including addition, subtraction, multiplication, division, modulus, and exponentiation.

2. What are the main arithmetic operators in JavaScript?

The main arithmetic operators are:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)
  • Exponentiation (**)
  • Increment (++)
  • Decrement (--)

3. What is the difference between the + and – operators?

The plus operator adds numeric values, while the minus operator subtracts one numeric value from another.

The plus operator can also join strings together.

4. What does the modulus operator do?

The modulus operator (%) returns the remainder after division.

For example:

console.log(15 % 4);

The output is 3.

5. What is the difference between increment and decrement?

The increment operator (++) increases a numeric value by one, while the decrement operator (--) decreases it by one.

6. Can I use arithmetic operators with variables?

Yes. Arithmetic operators can be used with variables that contain numeric values.

let price = 20;
let quantity = 3;

console.log(price * quantity);

The output is 60.

7. Why does JavaScript sometimes return unexpected decimal results?

JavaScript uses binary floating-point number representation. Some decimal fractions cannot be represented exactly, which can produce small precision differences in calculations.

Conclusion

JavaScript arithmetic operators are essential tools for anyone learning web development. They make it possible to perform mathematical calculations, work with numeric values, and build useful features into websites and applications.

In this tutorial, we covered all the main arithmetic operators, including addition, subtraction, multiplication, division, modulus, exponentiation, increment, and decrement.

We also explored practical examples, operator precedence, and common mistakes that beginners should understand.

The best way to master these operators is to practise them in your own code. Try changing the numbers in the examples, combining different operators, and creating a small calculator to test what you have learned.

Once you become comfortable with arithmetic operators, you will be ready to explore more JavaScript concepts and build more advanced projects.

Ready to become a better coder? Don’t miss this tutorial!  Age Calculator with Driving Licence Eligibility Check Using HTML, CSS & JavaScript

Leave a Reply