Sub topic 5
Inside the Number Type: NaN, -0, Infinity, and Precision
This chapter assumes you know the seven primitive types and can run code in a console. Every JavaScript number is a 64-bit IEEE 754 floating-point value. That format explains the rounding errors, the special values, and the integer limits covered here. Later chapters on conversion and equality depend on these cases.
Floating-Point Rounding
Binary floating point cannot store most decimal fractions exactly. 0.1 is stored as the nearest binary approximation, so sums of such values can be off by a tiny amount:
console.log(0.1 + 0.2); // 0.30000000000000004
console.log(0.1 + 0.2 === 0.3); // false
// Compare with a tolerance instead
const close = Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON;
console.log(close); // trueFor money, store whole cents as integers rather than fractional dollars.
NaN: The Value That Is Not Equal to Itself
NaN ("Not a Number") is a number value that results from failed numeric operations such as 0 / 0 or Number("abc"). It is the only value that is not equal to itself, so === cannot detect it.
console.log(typeof NaN); // "number"
console.log(NaN === NaN); // false
console.log(Number.isNaN(NaN)); // true
console.log(isNaN("abc")); // true: global isNaN converts "abc" to NaN first
console.log(Number.isNaN("abc")); // false: no conversion, "abc" is a stringNegative Zero
IEEE 754 has a sign bit, so -0 exists separately from 0. Strict equality treats them as equal. Object.is tells them apart, and so does division.
console.log(-0 === 0); // true
console.log(Object.is(-0, 0)); // false
console.log(1 / -0); // -Infinity
console.log(String(-0)); // "0": the sign disappears in strings-0 shows up in calculations like Math.round(-0.4). It rarely matters until you divide by it or check the sign of a direction.
Infinity
Overflow and division of a non-zero number by zero produce Infinity or -Infinity. These values do not throw errors.
console.log(1 / 0); // Infinity
console.log(Number.MAX_VALUE * 2); // Infinity
console.log(Infinity - Infinity); // NaN
console.log(Number.isFinite(Infinity)); // falseThe Safe Integer Limit
The 64-bit format has 53 bits of precision for the significand. Above Number.MAX_SAFE_INTEGER (2^53 − 1), the format can no longer represent every integer, so neighbouring integers round to the same value.
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
console.log(2 ** 53 === 2 ** 53 + 1); // true: precision is lost
console.log(Number.isSafeInteger(2 ** 53)); // falseDatabase IDs and timestamps in nanoseconds can exceed this limit. Use BigInt for them, as the next chapter explains.
Quick Reference
| Case | Wrong check | Reliable check |
|---|---|---|
| NaN | `x === NaN` | `Number.isNaN(x)` |
| -0 | `x === -0` | `Object.is(x, -0)` |
| Infinite | `x > 1e308` | `!Number.isFinite(x)` |
| Decimal equality | `a === b` | `Math.abs(a - b) < Number.EPSILON` |
| Large integers | `x <= 2 ** 53` | `Number.isSafeInteger(x)` |
Exercise
What do Number.isNaN(Number("12px")), Object.is(0 * -1, 0), and 9007199254740992 === 9007199254740993 return, and why?
Show answer
true, false, and true. Number("12px") is NaN, and Number.isNaN confirms it. 0 * -1 is -0, which Object.is tells apart from 0. Both large literals are above the safe integer limit and round to the same double, 2^53.
Exercise
A function receives value and must reject NaN but accept the string "NaN" without changing it. Which check should it use: isNaN(value) or Number.isNaN(value)?
Show answer
Number.isNaN(value). The global isNaN("NaN") converts the string to NaN and returns true, so it would reject the string. Number.isNaN returns true only for the actual NaN value.