Numbers and Arithmetic
How JavaScript represents numbers, the operators you use on them, and the surprising things floating-point arithmetic does.
Numbers are the most universal kind of value. We will spend this page learning what JavaScript means by "a number", what you can do with them, and the small set of gotchas worth knowing in advance.
All numbers are floating-point
Most programming languages have two numeric types: integers
(int) and decimals (float). JavaScript, for historical
simplicity, has just one:
Every JavaScript number is a 64-bit floating-point value (specifically, IEEE 754 double precision).
That means 7, 7.0, and 7.0000 are all the same value, the
same type, stored the same way. There is no distinction between
"integer" and "decimal" at the type level.
(There is a separate, very advanced type called bigint for
arbitrarily large integers, written like 42n. You can safely
ignore it for now.)
Basic arithmetic
The familiar operators all work as expected:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Add | 2 + 3 | 5 |
- | Subtract | 2 - 3 | -1 |
* | Multiply | 2 * 3 | 6 |
/ | Divide | 7 / 2 | 3.5 |
% | Remainder (modulo) | 7 % 3 | 1 |
** | Exponent | 2 ** 10 | 1024 |
Unary operators:
-x— negate.+x— coerce to number (mostly useful whenxis a string).
Order of operations
JavaScript uses ordinary mathematical precedence. *, /, %,
** bind more tightly than + and -. Use parentheses to make
intent obvious.
When in doubt, parenthesise. There is no prize for the shortest expression.
Compound assignment
When you want to update a variable based on its current value, you can use the compact compound-assignment operators:
| Operator | Equivalent to | Example |
|---|---|---|
+= | x = x + ... | x += 5 |
-= | x = x - ... | x -= 5 |
*= | x = x * ... | x *= 5 |
/= | x = x / ... | x /= 5 |
%= | x = x % ... | x %= 5 |
**= | x = x ** ... | x **= 2 |
Also: x++ is x = x + 1 (and returns the old value), x--
is x = x - 1. These are common in loops; we will see them often.
Special numeric values
JavaScript has three special numeric values you will eventually encounter:
Infinity— what you get when you divide a positive number by zero, or when a number gets too large to represent.-Infinity— same, the negative side.NaN— "Not a Number". The result of mathematically undefined operations like0 / 0orMath.sqrt(-1), or of trying to convert a non-numeric string to a number.
The NaN === NaN trap is one of the most famous quirks in
JavaScript. To check for NaN, use Number.isNaN(value). Never
use value === NaN.
Floating-point surprises
Because all JavaScript numbers are stored as 64-bit floating-point, some calculations that look exact actually have tiny rounding errors. The classic example:
This is not a JavaScript bug — it is how floating-point arithmetic
works in essentially every language (C, Java, Python, Rust, …).
The number 0.1 cannot be represented exactly in binary, any more
than 1/3 can be represented exactly in decimal.
What to do about it:
- For everyday code, this rarely matters. Display rounded values
(
(0.1 + 0.2).toFixed(2)→"0.30"). - When comparing decimals, compare with a small tolerance:
Math.abs(a - b) < 1e-9. - For money, never use floating point if you can help it. Store
amounts as integer cents (a $1.23 price becomes
123), do all arithmetic on integers, and only convert to dollars-with-decimals for display. This is what real e-commerce systems do.
Useful number tools
A few built-ins you will use constantly:
Notice the very important detail: toFixed returns a string,
not a number. This is a recurring beginner trap. If you write
x.toFixed(2) + 1, you get the string "1234.571", not the
number 1235.57. When you want to display a value, use
toFixed. When you want to compute with a value, keep it as a
number.
Integer-safe range
Because of floating point, JavaScript can represent integers
exactly only up to 2 ** 53 - 1 (9007199254740991). Beyond
that, you lose precision.
This rarely matters for everyday code. It does matter if you handle truly large numeric IDs (some databases produce them) or do heavy mathematics.
Putting it together: a small calculator
Below is a tiny "calculator" that exercises most of what we have learned. Try changing the inputs and see what happens.
Challenge
Write two functions:
toCelsius(f)— converts Fahrenheit to Celsius using(f - 32) * 5 / 9.toFahrenheit(c)— converts Celsius to Fahrenheit usingc * 9 / 5 + 32.
Both should return a number, not a string. The tests use a small tolerance to allow for floating-point rounding.
Why does 0.1 + 0.2 === 0.3 evaluate to false in JavaScript?
Because JavaScript has a bug in its addition operator
Because JavaScript numbers are 64-bit floating-point, and decimals like 0.1 cannot be represented exactly in binary — tiny rounding errors are unavoidable
Because 0.3 is actually null under the hood
Because === always returns false for decimals