JavaScript Numbers
JavaScript has a single number type for integers and decimals alike. Every number is stored as a 64-bit IEEE 754 floating-point value — the same "double" type that most other languages offer.
const whole = 42;
const decimal = 3.75;
const negative = -12;
const big = 2.5e6; // scientific notation: 2.5 × 10⁶
const small = 4e-3; // 0.004
const readable = 1_000_000; // underscores are just for readability
console.log(whole, decimal, negative, big, small, readable);42 3.75 -12 2500000 0.004 1000000
Integer precision
Integers are exact up to Number.MAX_SAFE_INTEGER (2⁵³ − 1, about 9 quadrillion). Beyond that, gaps appear between representable numbers:
console.log(Number.MAX_SAFE_INTEGER);
console.log(9007199254740992 + 1); // can't represent ...993
console.log(Number.isSafeInteger(2 ** 53));
console.log(999999999999999999); // 18 nines9007199254740991
9007199254740992
false
1000000000000000000
For bigger exact integers, use BigInt (next chapters).
Decimal precision
Decimal fractions like 0.1 can't be stored exactly in binary, so small rounding errors creep in. Compare decimals with a tolerance, or round before displaying:
const sum = 0.1 + 0.2;
console.log(sum);
console.log(sum === 0.3);
console.log(Math.abs(sum - 0.3) < Number.EPSILON);
console.log(sum.toFixed(2));
// Money tip: count in cents
const priceCents = 1999;
console.log((priceCents * 3) / 100);0.30000000000000004
false
true
0.30
59.97
Numbers and strings
+ with a string concatenates, but the other arithmetic operators try to convert strings to numbers:
console.log(10 + 20);
console.log("10" + 20);
console.log(10 + 20 + "30"); // 30 + "30"
console.log("100" / "4");
console.log("100" * 2, "100" - 1);
console.log("abc" * 2);30
1020
3030
25
200 99
NaN
NaN — Not a Number
NaN is the result of a failed numeric operation. Oddly, it's of type number, and it's the only value that isn't equal to itself — so check for it with Number.isNaN():
const bad = Number("twelve");
console.log(bad);
console.log(typeof bad);
console.log(bad === NaN); // never true!
console.log(Number.isNaN(bad));
console.log(bad + 5); // NaN spreads through calculationsNaN
number
false
true
NaN
Infinity
Dividing by zero, or exceeding the largest number (about 1.8 × 10³⁰⁸), gives Infinity or -Infinity:
console.log(1 / 0, -1 / 0);
console.log(Number.MAX_VALUE * 2);
console.log(Number.isFinite(1 / 0), Number.isFinite(42));Infinity -Infinity
Infinity
false true
Other bases
Numbers can be written in hexadecimal (0x), binary (0b) or octal (0o). toString(base) converts the other way:
| Literal | Base | Value |
|---|---|---|
0xff |
16 | 255 |
0b1010 |
2 | 10 |
0o17 |
8 | 15 |
(255).toString(16); // "ff"
(10).toString(2); // "1010"Note: Avoid
new Number(5)— likenew String, it creates a wrapper object, not a number, and breaks===comparisons.
Test yourself
What is "5" * "4"?
- "54"
- 20
- NaN
- "20"
How should you test whether x is NaN?
x === NaNx == NaNNumber.isNaN(x)typeof x === "NaN"
What is the result of 7 / 0?
- 0
- NaN
- Infinity
- An error