Arithmetic Operators in JavaScript

🧮 What are Arithmetic Operators?

Arithmetic operators in JavaScript allow you to perform mathematical calculations such as addition, subtraction, multiplication, and division. These are foundational in most programming logic, from simple math to complex algorithms.

>>“Math is the language of logic — and JavaScript speaks it fluently.” 🧠

📌 List of Arithmetic Operators

OperatorSymbolDescriptionExample
Addition+Adds two values5 + 3 // 8
Subtraction-Subtracts one value from another5 - 3 // 2
Multiplication*Multiplies values5 * 3 // 15
Division/Divides the left value by the right6 / 2 // 3
Modulus%Returns the remainder5 % 2 // 1
Exponentiation**Raises the first operand to the power of the second2 ** 3 // 8

🔢 Example Usage

Basic Arithmetic Examples

let a = 10;
let b = 3;

console.log(a + b);  // 13
console.log(a - b);  // 7
console.log(a * b);  // 30
console.log(a / b);  // 3.333...
console.log(a % b);  // 1
console.log(a ** b); // 1000

🧮 Compound Assignment

You can combine arithmetic with assignment using compound operators.

Using Compound Assignment

let x = 5;
x += 2; // Same as: x = x + 2
x -= 1; // Same as: x = x - 1
x *= 3; // Same as: x = x * 3
x /= 2; // Same as: x = x / 2
x %= 4; // Same as: x = x % 4
x **= 2; // Same as: x = x ** 2

Note

💡 Compound assignment operators make your code shorter and cleaner!

⚠️ Division and Modulus Notes

  • Division returns floating-point results by default.
  • Modulus returns the remainder (often used in loops, even-odd checks).

🌐 Useful Resources

>>“Mastering operators is like sharpening your programming sword — essential for every battle.” ⚔️