Arithematic Operator

Learn how to use JavaScript arithmetic operators like addition, subtraction, multiplication, division, and exponentiation with practical real-life examples.

What is an Arithmetic Operator?Link to this section

An arithmetic operator is a symbol used in programming to perform mathematical operations on numbers. They take two or more numerical values and produce a new numerical value as a result.

This example adds two numbers together and stores the result in a variable.

  • `10` is the first operand.
  • `+` is the arithmetic operator.
  • `5` is the second operand.
  • `score` stores the resulting value of 15.

Arithmetic operators form the fundamental basis of all mathematical calculations performed inside web applications and computer programs.

Why Do We Need Arithmetic Operators?Link to this section

Computers are powerful calculation engines, and arithmetic operators allow us to implement logic that handles real-world numerical data.

  • Calculate total shop bills including taxes and discounts
  • Compute student examination marks and averages
  • Track user scores in an online video game
  • Calculate monthly salaries and deductions for employees
  • Determine the current age of a user based on their birth year
  • Count items remaining in an inventory stock list

Here we calculate the final price of an item by adding its base price to the tax.

Without arithmetic operators, programs would only be able to store raw numbers without the ability to manipulate, combine, or analyze them.

About Arithmetic OperatorsLink to this section

Arithmetic operators work just like standard mathematics taught in school, following familiar rules of calculation. They evaluate expressions from left to right while respecting standard operator precedence. JavaScript provides a wide range of operators ranging from simple addition to advanced mathematical exponents.

Operator TypeSymbolExampleDescription
Addition+5 + 3Adds two values
Subtractionنسخه10 - 4Subtracts right operand from left
Multiplication*4 * 5Multiplies two values

Multiplication happens before subtraction due to standard operator precedence rules.

Types of JavaScript Arithmetic OperatorsLink to this section

1. Addition Operator (`+`)Link to this section

The addition operator adds two numbers together to produce their sum. It is also used in JavaScript for string concatenation when numbers are mixed with text.

warning

Be careful when adding strings and numbers together, as JavaScript will concatenate them instead of performing math.

tip

Always ensure your inputs are parsed as actual numbers using Number() or parseInt() if they come from text inputs.

2. Subtraction Operator (`-`)Link to this section

The subtraction operator subtracts the right operand from the left operand. It is essential for finding differences, remaining balances, or reductions.

note

Unlike the `+` operator, the `-` operator will attempt to convert string numbers into actual numbers automatically.

3. Multiplication Operator (`*`)Link to this section

The multiplication operator multiplies two numbers together. It is represented by the asterisk symbol and is heavily used in pricing calculations and scaling.

tip

Use parentheses when combining multiplication with addition or subtraction to make your code easier to read.

4. Division Operator (`/`)Link to this section

The division operator divides the left operand by the right operand. It returns a floating-point number if the division does not result in a whole number.

warning

Dividing any number by zero in JavaScript does not crash your program; it returns `Infinity`.

5. Remainder Operator (`%`)Link to this section

The remainder operator, often called modulo, returns the division remainder left over when one operand is divided by another. It is very useful for checking odd and even numbers.

note

The result of a modulo operation always takes the sign of the first operand.

6. Exponentiation Operator (`**`)Link to this section

The exponentiation operator raises the first operand to the power of the second operand. It provides a clean syntax alternative to the older Math.pow() method.

tip

Writing `5 ** 2` is much cleaner and easier to read than writing `Math.pow(5, 2)`.

7. Increment Operator (`++`)Link to this section

The increment operator increases a numeric value by exactly 1. It is frequently used inside loops to count iterations.

Post-increment: Evaluates the expression first, then increases the variable value by 1.

Pre-increment: Increases the variable value by 1 first, then evaluates the expression.

warning

Be careful with placement: postfix `x++` returns the value before incrementing, while prefix `++x` increments first and then returns.

8. Decrement Operator (`--`)Link to this section

The decrement operator decreases a numeric value by exactly 1. Just like incrementing, it can be used as prefix or postfix.

Post-decrement: Returns the original value before decreasing it by 1.

Pre-decrement: Decreases the value by 1 first, then returns the new value.

tip

Decrement operators are commonly used in countdown timers or loops running backwards.

Operator PrecedenceLink to this section

JavaScript follows mathematical order of operations. Multiplication, division, and exponentiation take higher precedence than addition and subtraction. You can use parentheses to override these default rules.

tip

When in doubt, always use parentheses to make complex mathematical expressions clear and unambiguous.

Complete Coding ExampleLink to this section

This script calculates a store purchase total with discount and checks if the total is an even number.

This complete program demonstrates multiplication, subtraction, and the remainder operator working together in a practical e-commerce scenario.

Important NotesLink to this section

  • Arithmetic operators perform standard mathematical calculations on numbers.
  • The `+` operator performs both numerical addition and string concatenation.
  • The `-`, `*`, and `/` operators attempt to convert strings into numbers automatically.
  • Division by zero does not crash JavaScript; it results in Infinity.
  • The remainder operator `%` gives you the leftover value after division.
  • Exponentiation `**` raises a base number to a specified power.
  • Increment `++` and decrement `--` operators modify variable values by 1.
  • Multiplication and division always happen before addition and subtraction.
  • Parentheses `()` can be used to override default operator precedence rules.
  • Always verify that your user inputs are numbers before performing calculations.

SummaryLink to this section

Arithmetic operators are the foundational building blocks for performing math in JavaScript applications. By mastering addition, subtraction, multiplication, division, and remainder operations, you can now process data effectively and solve real-world programming problems.

Check Your UnderstandingLink to this section

Javascript Arithematic Operator quiz
Question 1 of 6

What is the output of `console.log(10 + 5);`?

Practice ChallengesLink to this section

1. Calculate Total Price

easy

Create two variables representing the price of a notebook (15) and the quantity bought (4). Calculate the total cost and print it.

Expected output

60

Hint: Use the multiplication operator * between the price and quantity.

2. Calculate Remaining Budget

easy

You have a monthly salary of 1200 and you spend 450 on rent. Calculate and print your remaining budget.

Expected output

750

Hint: Use the subtraction operator - to subtract rent from salary.

3. Check Odd or Even

medium

Given a variable with a value of 17, use the remainder operator % to find the remainder when divided by 2, and print the result.

Expected output

1

Hint: Any odd number divided by 2 will leave a remainder of 1.

4. Calculate Average Marks

medium

Three students scored 80, 90, and 85 marks in an exam. Calculate their total sum and divide by 3 to find the average, then print it.

Expected output

85

Hint: Add all marks inside parentheses before dividing by 3.

5. Compound Calculation with Precedence

hard

Calculate the result of 10 + 5 * 2 - 3 ** 2 and print the final value.

Expected output

11

Hint: Evaluate exponentiation and multiplication first, then addition and subtraction.

Rate this content

Sign in to rate

Discussion

Sign in to join the discussion