JavaScript

JavaScript is a high-level, interpreted programming language that is widely used for web development. Initially designed as a client-side scripting language, it runs directly in web browsers, enabling dynamic and interactive user experiences. JavaScript can now be used for server-side development as well.

JavaScript Arithmetic Operators

2 April 2025 | Category:

Arithmetic operators in JavaScript are used to perform mathematical operations such as addition, subtraction, multiplication, division, modulus, and more. These operators work on numerical values and are essential for performing calculations in JavaScript.


1️⃣ Types of Arithmetic Operators in JavaScript

JavaScript provides the following arithmetic operators:

OperatorSymbolExampleDescription
Addition+5 + 38Adds two numbers
Subtraction-10 - 46Subtracts two numbers
Multiplication*6 * 212Multiplies two numbers
Division/8 / 24Divides two numbers
Modulus (Remainder)%10 % 31Returns remainder of division
Exponentiation**2 ** 38Raises a number to a power
Increment++let x = 5; x++6Increases a number by 1
Decrement--let y = 5; y--4Decreases a number by 1

2️⃣ Understanding Each Arithmetic Operator with Examples

1. Addition (+)

The + operator is used to add two numbers.

let a = 10;
let b = 5;
let sum = a + b;
console.log(sum); // Output: 15

🔹 With Strings:
The + operator is also used for string concatenation.

let firstName = "John";
let lastName = "Doe";
console.log(firstName + " " + lastName);  // Output: John Doe

2. Subtraction (-)

The - operator subtracts the second number from the first.

let x = 20;
let y = 7;
console.log(x - y);  // Output: 13

3. Multiplication (*)

The * operator multiplies two numbers.

let m = 6;
let n = 4;
console.log(m * n);  // Output: 24

4. Division (/)

The / operator divides the first number by the second.

let num1 = 10;
let num2 = 2;
console.log(num1 / num2);  // Output: 5

🔹 Floating-Point Division Example:

console.log(7 / 3);  // Output: 2.3333

5. Modulus (%)

The % operator returns the remainder after division.

console.log(10 % 3);  // Output: 1 (10 divided by 3 leaves remainder 1)
console.log(15 % 4);  // Output: 3 (15 divided by 4 leaves remainder 3)

🔹 Common Use Case: Checking if a number is even or odd.

let number = 7;
if (number % 2 === 0) {
    console.log("Even Number");
} else {
    console.log("Odd Number");
}
// Output: Odd Number

6. Exponentiation (**)

The ** operator raises a number to a power.

console.log(2 ** 3);  // Output: 8 (2³)
console.log(5 ** 2);  // Output: 25 (5²)

🔹 Equivalent Math.pow() Method:

console.log(Math.pow(2, 3));  // Output: 8

7. Increment (++)

The ++ operator increases a number by 1.

let count = 5;
count++;  
console.log(count);  // Output: 6

🔹 Post-Increment (x++) vs. Pre-Increment (++x):

let num = 5;
console.log(num++);  // Output: 5 (returns value first, then increments)
console.log(num);    // Output: 6

let val = 5;
console.log(++val);  // Output: 6 (increments first, then returns value)

8. Decrement (--)

The -- operator decreases a number by 1.

let count = 10;
count--;  
console.log(count);  // Output: 9

🔹 Post-Decrement (x--) vs. Pre-Decrement (--x):

let num = 10;
console.log(num--);  // Output: 10 (returns value first, then decrements)
console.log(num);    // Output: 9

let val = 10;
console.log(--val);  // Output: 9 (decrements first, then returns value)

3️⃣ Operator Precedence (Order of Execution)

JavaScript follows BODMAS (Bracket, Order, Division, Multiplication, Addition, Subtraction) rule.

OperatorPrecedence (Higher to Lower)
()Parentheses (Highest)
**Exponentiation
*, /, %Multiplication, Division, Modulus
+, -Addition, Subtraction

🔹 Example:

console.log(10 + 5 * 2);  // Output: 20 (Multiplication first, then addition)
console.log((10 + 5) * 2);  // Output: 30 (Parentheses first)

4️⃣ Special Cases in Arithmetic Operations

Arithmetic Operations with Strings

If a string is used with +, JavaScript performs string concatenation instead of arithmetic addition.

console.log("10" + 5);  // Output: "105" (String concatenation)
console.log(10 + "5");  // Output: "105"

However, other arithmetic operations convert strings to numbers automatically.

console.log("10" - 5);  // Output: 5
console.log("10" * 2);  // Output: 20
console.log("10" / 2);  // Output: 5

Arithmetic with true and false

JavaScript treats true as 1 and false as 0.

console.log(true + 5);  // Output: 6 (true = 1)
console.log(false + 5);  // Output: 5 (false = 0)

Division by Zero

JavaScript allows division by zero but returns Infinity.

console.log(10 / 0);  // Output: Infinity
console.log(-10 / 0);  // Output: -Infinity

NaN (Not a Number)

Invalid arithmetic operations return NaN.

console.log("hello" * 3);  // Output: NaN
console.log(0 / 0);  // Output: NaN

🚀 Summary

✔️ Arithmetic operators perform basic math operations.
✔️ +, -, *, /, %, ** for calculations.
✔️ Increment (++) and decrement (--) change values by 1.
✔️ Operator precedence follows BODMAS rules.
✔️ Special behavior when used with strings, booleans, or division by zero.