JavaScript Arithmetic Operators ๐ข

JavaScript Arithmetic Operators ๐ข
Arithmetic operators in JavaScript are used to perform mathematical operations on numbers.
1๏ธโฃ List of Arithmetic Operators
Operator Symbol Example Result
Addition + 5 + 2 7
Subtraction - 5 - 2 3
Multiplication * 5 * 2 10
Division / 5 / 2 2.5
Modulus (Remainder) % 5 % 2 1
Exponentiation ** 5 ** 2 25
2๏ธโฃ Examples of Arithmetic Operators
โ
1. Addition (+)
Adds two numbers.
let sum = 10 + 5;
console.log(sum); // 15
console.log("Hello" + " World"); // "Hello World"
console.log("10" + 5); // "105" (Number 5 is converted to a string)
let difference = 10 - 5;
console.log(difference); // 5
let product = 4 * 3;
console.log(product); // 12
let quotient = 10 / 2;
console.log(quotient); // 5
console.log(10 / 3); // 3.3333...
console.log(10 % 3); // 1 (10 รท 3 = 3 remainder 1)
console.log(15 % 4); // 3 (15 รท 4 = 3 remainder 3)
let num = 7;
console.log(num % 2 === 0 ? "Even" : "Odd"); // "Odd"
console.log(2 ** 3); // 8 (2ยณ)
console.log(5 ** 2); // 25 (5ยฒ)
console.log(Math.pow(2, 3)); // 8
console.log(10 / 0); // Infinity
console.log(-10 / 0); // -Infinity
๐ Note: JavaScript returns Infinity instead of throwing an error.
โ 2. NaN (Not a Number)
console.log("hello" * 5); // NaN
console.log(undefined + 1); // NaN
๐ Tip: Use isNaN(value) to check if a result is NaN.
๐ฏ Summary: Best Practices
โ Use +, -, *, /, %, ** for mathematical operations.
โ Watch out for type coercion with + when using strings.
โ Use % to check even or odd numbers.
โ Avoid operations that result in NaN.Date: 2025-03-29 00:00:00.000000