JavaScript

coding learning websites codepractice

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Arithmetic


In JavaScript, arithmetic operations are essential for performing calculations and handling numerical data. Arithmetic operators allow developers to add, subtract, multiply, divide, calculate remainders, perform exponentiation, and increment or decrement values. Mastering these operators is crucial for creating applications that involve calculations, form inputs, scores, statistics, and more.

This chapter explains all arithmetic operators in detail, demonstrates practical examples using variables, shows operator precedence, highlights common mistakes, and includes real-world applications.

What Are Arithmetic Operators

Arithmetic operators are symbols that perform calculations on numeric values or variables. They include addition, subtraction, multiplication, division, modulus, exponentiation, increment, and decrement. These operators can be used with numbers directly, variables storing numbers, or expressions that result in numeric values.

The primary arithmetic operators in JavaScript are:

Operator Description Example
+ Addition 5 + 3 → 8
- Subtraction 5 - 3 → 2
* Multiplication 5 * 3 → 15
/ Division 15 / 3 → 5
% Modulus (Remainder) 5 % 3 → 2
** Exponentiation (Power) 2 ** 3 → 8
++ Increment (adds 1) let a = 5; a++; → 6
-- Decrement (subtracts 1) let a = 5; a--; → 4

These operators are widely used in calculations for applications, loops, conditional statements, and more.

Addition Operator (+)

The addition operator + is used to add numeric values or concatenate strings. When adding numbers, it performs numeric addition.

let ageAnanya = 22;
let ageIshita = 20;
let totalAge = ageAnanya + ageIshita;
console.log("Total age:", totalAge); // 42

When used with strings, the + operator combines them:

let firstName = "Sanya";
let lastName = "Riya";
let fullName = firstName + " " + lastName;
console.log("Full Name:", fullName); // Sanya Riya

Be careful when combining numbers and strings, as JavaScript will convert numbers to strings in concatenation.

Subtraction Operator (-)

The subtraction operator - calculates the difference between two numeric values.

let ageMira = 25;
let ageDiya = 21;
let difference = ageMira - ageDiya;
console.log("Age difference:", difference); // 4

Subtraction is strictly numeric; it does not work on string values without conversion.

Multiplication Operator (*)

The multiplication operator * multiplies numeric values.

let pricePerBook = 250;
let numberOfBooks = 4;
let totalPrice = pricePerBook * numberOfBooks;
console.log("Total Price:", totalPrice); // 1000

Multiplication is useful for scaling quantities, calculating areas, or performing repetitive calculations.

Division Operator (/)

The division operator / divides one number by another.

let totalMarks = 480;
let numberOfStudents = 5;
let averageMarks = totalMarks / numberOfStudents;
console.log("Average Marks:", averageMarks); // 96

Dividing by zero produces Infinity in JavaScript, which should be handled carefully.

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

Modulus Operator (%)

The modulus operator % returns the remainder of a division. This is commonly used to check even or odd numbers or in cyclic operations.

let ageRiya = 23;
console.log("Riya is " + (ageRiya % 2 === 0 ? "even-aged" : "odd-aged")); // odd-aged

Exponentiation Operator (**)

The exponentiation operator ** raises a number to the power of another number.

let base = 2;
let exponent = 3;
let result = base ** exponent;
console.log("2 to the power 3 is:", result); // 8

This operator is useful for calculations in growth, interest rates, physics, and other scientific applications.

Increment (++) and Decrement (--)

The increment operator ++ increases a variable by 1, and the decrement operator -- decreases it by 1. These operators are often used in loops or counters.

let scoreAnanya = 10;
scoreAnanya++;
console.log("Score after increment:", scoreAnanya); // 11

scoreAnanya--;
console.log("Score after decrement:", scoreAnanya); // 10

The prefix and postfix forms behave slightly differently:

let x = 5;
console.log(++x); // Prefix: increments then prints → 6
console.log(x++); // Postfix: prints then increments → 6
console.log(x);   // Now x = 7

Operator Precedence

Arithmetic operations follow a hierarchy called operator precedence. Multiplication, division, and modulus have higher precedence than addition and subtraction. Parentheses can override the default order.

let result = 10 + 5 * 2;
console.log(result); // 20, multiplication first

let correctResult = (10 + 5) * 2;
console.log(correctResult); // 30

Practical Examples

Example 1: Total Age Calculation

let ageAnanya = 22;
let ageIshita = 20;
let ageSanya = 21;
let totalAge = ageAnanya + ageIshita + ageSanya;
console.log("Total Age:", totalAge); // 63

Example 2: Average Score

let scores = [85, 92, 78, 88, 90];
let total = scores[0] + scores[1] + scores[2] + scores[3] + scores[4];
let average = total / scores.length;
console.log("Average Score:", average); // 86.6

Example 3: Price Calculation

let priceBook = 250;
let pricePen = 50;
let totalPrice = priceBook * 3 + pricePen * 5;
console.log("Total Price:", totalPrice); // 1050

Example 4: Even or Odd Check

let ageMira = 24;
console.log(ageMira % 2 === 0 ? "Even Age" : "Odd Age"); // Even Age

Example 5: Loop with Increment

let points = 0;
for (let i = 1; i <= 5; i++) {
    points += i;
}
console.log("Total Points:", points); // 15

Common Mistakes

  • Ignoring operator precedence in complex expressions.

  • Using arithmetic on strings without converting them to numbers.

  • Misunderstanding the difference between prefix and postfix increment.

  • Dividing by zero without handling, resulting in Infinity.

Best Practices

  • Use parentheses to clarify complex calculations.

  • Ensure numeric types before performing arithmetic.

  • Use descriptive variable names for clarity.

  • Test calculations with sample data to ensure correctness.

Real-World Applications

Arithmetic operators are used in everyday programming tasks, including:

  • Calculating totals in shopping carts

  • Computing averages, scores, and statistics in apps

  • Financial calculations like tax, interest, and discounts

  • Managing counters, points, or levels in games

  • Performing measurements, units, or scientific calculations

Summary of JS Arithmetic

JavaScript arithmetic operators allow developers to perform calculations on numeric data. These include addition, subtraction, multiplication, division, modulus, exponentiation, and increment/decrement. Understanding operator behavior, precedence, and proper usage is essential for accurate calculations. This tutorial included detailed explanations, practical examples, loops, and real-world scenarios. Mastery of arithmetic operations is crucial for building functional, dynamic, and reliable applications.


Practice Questions

Q1. How do you add two variables a = 10 and b = 15 and store the result in sum?

Q2. How do you subtract variable b = 5 from a = 20 and print the result?

Q3. How do you multiply x = 4 and y = 5 using the * operator?

Q4. How do you divide 25 by 4 using the / operator and print the result?

Q5. How do you find the remainder when 15 is divided by 4 using the % operator?

Q6. How do you calculate 3 raised to the power of 4 using the ** operator?

Q7. How do you increment a variable counter by 1 using both counter++ and ++counter?

Q8. How do you decrement a variable level from 10 to 9 using the decrement operator?

Q9. How do you use arithmetic operators to calculate the average of three numbers a = 5, b = 10, c = 15?

Q10. How do you use parentheses to ensure that addition occurs before multiplication in the expression 5 + 3 * 2?


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

JavaScript

online coding class codepractice

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

Go Back Top