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
| Operator | Symbol | Description | Example |
|---|---|---|---|
| Addition | + | Adds two values | 5 + 3 // 8 |
| Subtraction | - | Subtracts one value from another | 5 - 3 // 2 |
| Multiplication | * | Multiplies values | 5 * 3 // 15 |
| Division | / | Divides the left value by the right | 6 / 2 // 3 |
| Modulus | % | Returns the remainder | 5 % 2 // 1 |
| Exponentiation | ** | Raises the first operand to the power of the second | 2 ** 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 ** 2Note
💡 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.” ⚔️