Basics
Arithmetic operators
Addition, subtraction, multiplication, division and the shortcuts ++ and +=.
Why it exists
Every app calculates something: an order total, a discount, progress in percent, positions on screen, time until a sale ends. Arithmetic in JavaScript looks like school maths, but it has quirks — numbers are stored in binary, and + can also join strings. Knowing these quirks is what keeps a cart from showing "$10050" instead of "$150".
Where it's used
- Money: cart total, discount, tax, change.
- Counters:
likes++,total += price. - Interfaces: column widths, a progress bar
done / total * 100. - Games: score, health, a character's speed and position.
Syntax
a + b a - b a * b a / b
count++ // count = count + 1
total += 5 // total = total + 5Examples
7 * 3; // → 21
10 / 4; // → 2.5
"2" + 3; // → "23" — string joining!
let steps = 0;
steps++;
steps; // → 1const price = 1200;
const count = 3;
const discount = 0.1;
price * count * (1 - discount); // → 3240"5" - 2; // → 3
"5" + 2; // → "52"
"5" * "2"; // → 10+ joins — the other operators turn strings into numbers0.1 + 0.2; // → 0.30000000000000004
(10 + 20) / 100; // → 0.310 / 0; // → Infinity
0 / 0; // → NaN
Math.round(2.5); // → 3Results after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
JavaScript has one numeric type, number, for both whole numbers and fractions. Inside it's a binary fraction (the IEEE 754 standard), so some decimal fractions are stored inexactly: 0.1 + 0.2 gives 0.30000000000000004. That's why money is often counted in cents — as whole numbers.
The + operator is special: if at least one operand is a string, it joins strings. The other operators (-, *, /) always try to turn their operands into numbers.
Dividing by zero doesn't throw an error: you get Infinity, and meaningless operations give NaN ("not a number").
x++ is short for x = x + 1, and x += 5 for x = x + 5. There are also --, -=, *=, /=.
Traps
How it differs from…
- Joining strings
The same
+joins strings. The types decide what happens: number + number is addition; anything with a string is joining.- Remainder
% % is an arithmetic operator too, but it gives the remainder of a division rather than the quotient.
When to pick something else
For exact money calculations with fractions, don't rely on plain decimals — store amounts in cents (whole numbers). For very large integers (above 9 quadrillion) you need the BigInt type.