Number constants (MAX_SAFE_INTEGER, EPSILON…)
Every JavaScript number is a 64-bit float, including the ones that look like integers. These constants mark where that representation starts to lose information.
Demo
The pair is [is this a safe integer, does it equal itself plus one]. At MAX_SAFE_INTEGER the answer is [true, false] — everything behaves. Add one and it becomes [false, true]: the number is no longer exactly representable, so adding 1 produces the same value back. That is not a rounding display issue; the addition genuinely has no effect. Any id, timestamp in nanoseconds, or accumulated counter that crosses this line starts silently merging distinct values, which is why large integers from a backend should arrive as strings or be handled as BigInt.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| MAX_SAFE_INTEGER | number | no (9007199254740991) | 2⁵³ − 1. The largest integer for which every integer up to it is exactly representable. MIN_SAFE_INTEGER is its negative. |
| EPSILON | number | no (2.22e-16) | The gap between 1 and the next representable number — the standard tolerance for comparing floats near 1. |
| MAX_VALUE | number | no (1.79e+308) | The largest finite number. Anything above it is Infinity. MIN_VALUE is the smallest positive, about 5e-324. |
Return value
number — Each is a fixed numeric property describing a limit of the IEEE-754 double that every JavaScript number is.
Common patterns
const equal = Math.abs(a - b) < Number.EPSILON;
const big = 9007199254740993n;
const id = "9007199254740993"; // never parse it
Examples
Pitfalls
JSON.parse('{"id": 9007199254740993}').id
JSON.parse('{"id": "9007199254740993"}').id
Math.abs(1e9 - (1e9 + 1)) < Number.EPSILON
Math.abs(a - b) < Number.EPSILON * Math.max(Math.abs(a), Math.abs(b))
Number.MIN_VALUE
-Number.MAX_VALUE
Number.MAX_VALUE * 2
Number.isFinite(result)
When to use
- Bounds-checking integers before they lose precision
- Tolerance-based float comparison, scaled appropriately
- Detecting overflow after a large computation
- Documenting why a value is handled as a string or BigInt
- Integers beyond the safe range → BigInt, or strings
- Money → integer minor units
- Exact decimal arithmetic → a decimal library
- Comparing large floats → a relative tolerance, not EPSILON alone
Notes
FAQ
Because neither 0.1 nor 0.2 can be written exactly in binary, any more than a third can be written exactly in decimal. Each is stored as the nearest double, and the two errors do not cancel. The result is 0.30000000000000004 — correct for the values that actually exist.
0.1 + 0.2; // 0.30000000000000004 Math.abs((0.1 + 0.2) - 0.3) < Number.EPSILON; // true