When learning JavaScript, one of the first things you need to understand is how to perform mathematical calculations. Whether you are building a calculator, creating a shopping cart, calculating prices, or developing a simple web application, arithmetic operations are an essential part of programming.
JavaScript makes mathematical calculations straightforward with the help of arithmetic operators. These operators allow you to add, subtract, multiply, divide, and perform other mathematical operations using numbers.
In this tutorial, we will explore arithmetic operators in JavaScript, understand how each one works, and look at practical examples that you can try in your own projects.
If you are new to JavaScript, don’t worry. Everything is explained step by step in simple language, so you can understand the concepts and start using them confidently.
Watch the YouTube Video Tutorial
Before exploring the examples, watch the short video below to see the topic demonstrated in action.
Watch: Arithmetic Operators in JavaScript
The video provides a quick introduction, while this article explains each operator in more detail with additional examples.
What Are Arithmetic Operators in JavaScript?
Arithmetic operators are special symbols that JavaScript uses to perform mathematical calculations.
You probably already know some of these symbols from everyday mathematics, such as the plus sign (+), minus sign (-), and multiplication sign (×).
JavaScript uses similar symbols, although multiplication and division have slightly different symbols in code.
For example:
let a = 10;
let b = 5;
console.log(a + b);
Output:
15
In this example, the plus operator adds two numbers together and displays the result.
Arithmetic operators are commonly used in calculations involving prices, quantities, percentages, scores, measurements, and many other values.
Types of Arithmetic Operators in JavaScript
JavaScript provides several arithmetic operators. Each operator performs a different mathematical operation.
Here is a quick overview:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 5 | 2 |
% | Remainder | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
++ | Increment | a++ | Increases by 1 |
-- | Decrement | a-- | Decreases by 1 |
Let’s understand each arithmetic operator with practical JavaScript examples.
1. Addition Operator (+) in JavaScript
The addition operator (+) is used to add two or more numbers together.
It is one of the most commonly used arithmetic operators in JavaScript.
Example:
let a = 20;
let b = 10;
let result = a + b;
console.log(result);
Output:
30
In this example, we created two variables named a and b. The addition operator combines their values and stores the result in the result variable.
You can also add multiple numbers:
let total = 10 + 20 + 30 + 40;
console.log(total);
Output:
100
Important: Addition Can Also Join Strings
In JavaScript, the plus operator can also concatenate strings, which means joining text together.
For example:
let firstName = "John";
let lastName = "Smith";
console.log(firstName + " " + lastName);
Output:
John Smith
This is useful when displaying names, messages, and other text-based information.
2. Subtraction Operator (-) in JavaScript
The subtraction operator (-) is used to subtract one number from another.
For example, you can use it to calculate the remaining balance after a purchase or find the difference between two values.
Example:
let a = 50;
let b = 20;
let result = a - b;
console.log(result);
Output:
30
Here, JavaScript subtracts 20 from 50 and returns 30.
You can also use subtraction with variables:
let originalPrice = 100;
let discount = 25;
let finalPrice = originalPrice - discount;
console.log(finalPrice);
Output:
75
This type of calculation is especially useful when developing online shopping websites or price calculators.
3. Multiplication Operator (*) in JavaScript
The multiplication operator (*) is used to multiply two or more numbers.
In JavaScript, you must use the asterisk symbol (*) instead of the multiplication symbol (×).
Example:
let a = 10;
let b = 5;
let result = a * b;
console.log(result);
Output:
50
JavaScript multiplies 10 by 5 and returns 50.
Practical Example: Calculate the Total Price
Imagine you are building a shopping cart where customers can purchase multiple products.
You can calculate the total price by multiplying the product price by its quantity.
let productPrice = 15;
let quantity = 4;
let totalPrice = productPrice * quantity;
console.log(totalPrice);
Output:
60
The customer purchases four products, each costing 15, so the total price is 60.
4. Division Operator (/) in JavaScript
The division operator (/) is used to divide one number by another.
For example, you can use division to calculate an average, split a total amount, or determine the price of a single item.
Example:
let a = 100;
let b = 5;
let result = a / b;
console.log(result);
Output:
20
In this example, JavaScript divides 100 by 5 and returns 20.
Example: Calculate an Average
Let’s say a student receives marks in three subjects. You can calculate the average by adding all the marks and dividing the total by three.
let math = 80;
let science = 90;
let english = 70;
let average = (math + science + english) / 3;
console.log(average);
Output:
80
This is a simple example of how division can be used in real-world applications.
What happens when you divide by zero?
JavaScript does not throw a division-by-zero error for ordinary numeric division. Instead, a nonzero number divided by zero returns Infinity, while zero divided by zero returns NaN.
For example:
console.log(10 / 0);
console.log(0 / 0);
Output:
Infinity
NaN
When building a calculator, it is a good idea to check the divisor before performing division.
5. Modulus Operator (%) in JavaScript
The modulus operator (%) returns the remainder after dividing one number by another.
This operator is particularly useful when you need to check whether a number is even or odd, work with repeating patterns, or divide items into groups.
Example:
let a = 10;
let b = 3;
let result = a % b;
console.log(result);
Output:
1
Why does JavaScript return 1?
Because 10 divided by 3 gives a whole-number quotient of 3, with 1 remaining.
Practical Example: Check Even and Odd Numbers
One common use of the modulus operator is checking whether a number is even or odd.
let number = 8;
if (number % 2 === 0) {
console.log("Even Number");
} else {
console.log("Odd Number");
}
Output:
Even Number
If a number can be divided by 2 without leaving a remainder, it is even. Otherwise, it is odd.
This is a useful technique for beginners to understand because it combines arithmetic operators with conditional statements.
6. Exponentiation Operator (**) in JavaScript
The exponentiation operator (**) is used to raise one number to the power of another.
In simple terms, it allows you to multiply a number by itself a specified number of times.
Example:
let a = 2;
let b = 3;
let result = a ** b;
console.log(result);
Output:
8
The calculation is:
2 × 2 × 2 = 8.
Another example:
console.log(5 ** 2);
Output:
25
Here, 5 is multiplied by itself twice.
The exponentiation operator is useful in mathematical applications, scientific calculations, and projects involving powers.
7. Increment Operator (++) in JavaScript
The increment operator (++) increases a variable’s numeric value by one.
It is often used in loops, counters, and situations where you need to increase a number gradually.
Example:
let number = 5;
number++;
console.log(number);
Output:
6
The value starts at 5. After using the increment operator, it becomes 6.
Difference Between Prefix and Postfix Increment
JavaScript supports two ways to use the increment operator.
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, 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 variable first and then uses the updated value in the expression.
Understanding this difference can help you avoid unexpected results when working with counters and calculations.
8. Decrement Operator (–) in JavaScript
The decrement operator (–) does the opposite of increment.
It decreases a variable’s numeric value by one.
Example:
let number = 10;
number--;
console.log(number);
Output:
9
The original value is 10. After applying the decrement operator, it becomes 9.
Prefix and Postfix Decrement
Like increment, decrement can also be used in 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 commonly used when working with loops, countdowns, and changing numeric values.
Understanding Arithmetic Operator Precedence
When multiple arithmetic operators appear in the same expression, JavaScript follows a specific order of operations.
For example:
let result = 10 + 5 * 2;
console.log(result);
Output:
20
You might expect the result to be 30 if you calculate from left to right. However, multiplication has a higher precedence than addition.
JavaScript first calculates:
5 × 2 = 10
Then it adds:
10 + 10 = 20
You can use parentheses to control the order of calculations.
let result = (10 + 5) * 2;
console.log(result);
Output:
30
The parentheses tell JavaScript to perform the addition first.
Understanding operator precedence is important when writing longer mathematical expressions because it helps you avoid incorrect calculations.
Common Mistakes When Using Arithmetic Operators
Arithmetic operators are easy to understand, but there are a few mistakes beginners should watch out for.
1. Using the Wrong Multiplication Symbol
JavaScript does not use the mathematical multiplication symbol (×) in ordinary arithmetic expressions.
Incorrect:
let result = 5 × 2;
Correct:
let result = 5 * 2;
Always use the asterisk (*) for multiplication.
2. Confusing Division and 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
They perform different operations, so make sure you use the correct one.
3. Forgetting Operator Precedence
Consider this expression:
let result = 10 + 5 * 2;
The result is 20, not 30, because multiplication happens before addition.
Use parentheses when you want to make the intended calculation order clear.
4. Unexpected Results With Decimal Numbers
JavaScript uses floating-point numbers for ordinary numeric calculations. Because of this, some decimal calculations may produce results that look slightly inaccurate.
For example:
console.log(0.1 + 0.2);
Output:
0.30000000000000004
This happens because many decimal fractions cannot be represented exactly in binary floating-point format.
For simple financial displays, you can format a number using toFixed():
let price = 0.1 + 0.2;
console.log(price.toFixed(2));
Output:
0.30
Remember that toFixed() returns a string. For applications that handle real financial transactions, use appropriate decimal arithmetic or integer-based calculations rather than relying on floating-point values alone.
Frequently Asked Questions
1. What are arithmetic operators in JavaScript?
Arithmetic operators are symbols used to perform mathematical calculations, such as addition, subtraction, multiplication, and division.
2. How many arithmetic operators are there in JavaScript?
JavaScript commonly teaches eight basic arithmetic operators: addition, subtraction, multiplication, division, modulus, exponentiation, increment, and decrement.
3. What does the % operator do in JavaScript?
The modulus operator (%) returns the remainder after division.
For example:
console.log(10 % 3);
The result is 1.
4. What is the difference between / and % in JavaScript?
The division operator (/) returns the quotient, while the modulus operator (%) returns the remainder.
5. What is the difference between ++number and number++?
++number increases the variable before its value is used in the expression. number++ uses the original value first and increases the variable afterward.
6. Can arithmetic operators work with variables?
Yes. You can use arithmetic operators with variables containing numeric values.
let a = 20;
let b = 10;
console.log(a + b);
console.log(a - b);
console.log(a * b);
7. What is the exponentiation operator in JavaScript?
The exponentiation operator (**) raises a number to a specified power.
For example, 2 ** 3 returns 8.
Conclusion
Arithmetic operators are among the most important concepts to learn when starting with JavaScript. They allow you to perform mathematical calculations and build useful functionality into your web applications.
Throughout this tutorial, we explored addition, subtraction, multiplication, division, modulus, exponentiation, increment, and decrement operators. We also looked at practical examples, operator precedence, and common mistakes that beginners should understand.
The best way to become comfortable with these operators is to practise them yourself. Try changing the values in the examples, combining different operators, and creating a small calculator.
Once you understand how arithmetic operators work, you will have a stronger foundation for learning more advanced JavaScript concepts and building interactive web projects.
Keep practising and continue exploring more JavaScript tutorials to improve your coding skills.
Ready to become a better coder? Don’t miss this tutorial! Age Calculator with Driving Licence Eligibility Check Using HTML, CSS & JavaScript
