JavaScript Arithmetic Operators: Do Math in Your Programs
Every app does math — an e-commerce site calculates totals, a game tracks scores, a dashboard computes averages. JavaScript's arithmetic operators are how you make numbers work for you. They're simple to learn and open the door to building anything that involves numerical data.
The 6 Arithmetic Operators in JavaScript
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 3 | 10 |
- | Subtraction | 7 - 3 | 4 |
* | Multiplication | 7 * 3 | 21 |
/ | Division | 7 / 2 | 3.5 |
% | Modulus (remainder) | 7 % 3 | 1 |
** | Exponentiation | 2 ** 8 | 256 |
Using Operators With Variables
Operators work with raw numbers or variables — JavaScript substitutes the variable's current value:
let price = 49.99;
let quantity = 3;
let subtotal = price * quantity;
console.log(subtotal); // 149.97
let discount = subtotal * 0.1;
let total = subtotal - discount;
console.log(total.toFixed(2)); // "134.97"
The Modulus Operator: More Useful Than You Think
% returns the remainder after division. It's used constantly in real code:
// Even or odd?
console.log(10 % 2); // 0 → even
console.log(7 % 2); // 1 → odd
// Distribute items into rows of 4:
const items = 29;
const perRow = 4;
console.log(`Full rows: ${Math.floor(items / perRow)}`); // 7
console.log(`Leftover: ${items % perRow}`); // 1
// Convert seconds to minutes and seconds:
const totalSeconds = 137;
const mins = Math.floor(totalSeconds / 60);
const secs = totalSeconds % 60;
console.log(`${mins}m ${secs}s`); // 2m 17s
Order of Operations (PEMDAS)
JavaScript follows standard math precedence. Use parentheses to be explicit:
console.log(2 + 3 * 4); // 14 (multiplication first)
console.log((2 + 3) * 4); // 20 (parentheses first)
// Calculate average — parentheses matter:
const a = 10, b = 20, c = 30;
const wrong = a + b + c / 3; // 43.33 — divides only c by 3!
const correct = (a + b + c) / 3; // 20 — divides the sum
Real-World Examples
// Compound interest: A = P(1 + r)^t
const principal = 1000;
const rate = 0.05;
const years = 10;
const amount = principal * (1 + rate) ** years;
console.log(amount.toFixed(2)); // 1628.89
// Temperature conversion:
const celsius = 30;
const fahrenheit = (celsius * 9/5) + 32;
console.log(fahrenheit); // 86
// Calculate grade percentage:
const earned = 87;
const possible = 100;
const grade = (earned / possible) * 100;
console.log(`${grade}%`); // 87%
JavaScript-Specific Gotchas
+with strings concatenates, not adds:"5" + 3gives"53", not8. Always ensure your values are numbers before doing math.- Floating-point precision:
0.1 + 0.2gives0.30000000000000004in JavaScript (a quirk of binary floating-point). Use.toFixed(2)for display or multiply by 100 for money calculations. - Division never throws:
10 / 0returnsInfinity, not an error. Check your denominators.
// String + number:
console.log("5" + 3); // "53" (string concatenation!)
console.log(Number("5") + 3); // 8 (correct)
console.log(+"5" + 3); // 8 (unary + converts to number)
// Float precision:
console.log(0.1 + 0.2); // 0.30000000000000004
console.log((0.1 + 0.2).toFixed(2)); // "0.30"
Key Takeaways
+,-,*,/work as expected;%gives the remainder;**raises to a power.- JavaScript follows standard operator precedence — use parentheses to be explicit.
- The
+operator concatenates strings — convert to numbers first when doing math. - Floating-point results aren't always exact — use
.toFixed()for display.
What to Learn Next
Now that you can compute values, explore assignment operators (+=, -=, *=) to update variables concisely, and comparison operators (===, >, <) to start making decisions with if statements.
