Example 1 — Read Number.MAX_SAFE_INTEGER
Print the largest exact integer Number.
Number.MAX_SAFE_INTEGER;
// 9007199254740991 How It Works
Same as 2 ** 53 - 1. Prefer the named constant in readable code.
Number.MAX_SAFE_INTEGER is a static data property on Number (same API as MDN Number.MAX_SAFE_INTEGER). It is the largest integer a JavaScript Number can represent exactly — 253 − 1 (9007199254740991). Beyond it, use BigInt. This tutorial covers its value, the famous +1 === +2 trap, how it relates to EPSILON and MAX_VALUE, five examples, and try-it labs.
Static property
253−1
Exact integers
Use BigInt
No
Everywhere
JavaScript Numbers are IEEE-754 doubles. They can be enormous ((MAX_VALUE), but they cannot label every integer exactly once the magnitude grows. The 52-bit fraction (plus an implicit leading 1) gives 53 bits of integer precision.
Number.MAX_SAFE_INTEGER is 253 − 1 — the top of the range where consecutive integers are still distinct and comparable. Past that point, some integers are skipped.
Static properties belong to Number itself. Always write Number.MAX_SAFE_INTEGER. There is no (5).MAX_SAFE_INTEGER, and you do not call it like a function.
This page is part of JavaScript Number Properties. Related tools include Number.isSafeInteger(), Number.EPSILON, and Number.MAX_VALUE.
MAX_SAFE_INTEGER Property“Safe” means two things: the integer can be stored exactly, and integer comparisons / increments behave as you expect. Outside −MAX_SAFE_INTEGER … MAX_SAFE_INTEGER, that guarantee breaks.
9007199254740991 (253 − 1).Number.MIN_SAFE_INTEGER for the negative bound (same magnitude).General form of the static property Number.MAX_SAFE_INTEGER:
Number.MAX_SAFE_INTEGER 9007199254740991 (253 − 1).
| Attribute | Value |
|---|---|
| Writable | false |
| Enumerable | false |
| Configurable | false |
Number.MAX_SAFE_INTEGER; // 9007199254740991
Number.MAX_SAFE_INTEGER === 2 ** 53 - 1; // true
Number.isSafeInteger(Number.MAX_SAFE_INTEGER); // true
Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1); // false
// Prefer BigInt past the safe range:
9007199254740993n; // exact | Goal | Code |
|---|---|
| Largest exact integer | Number.MAX_SAFE_INTEGER |
| Smallest exact integer | Number.MIN_SAFE_INTEGER |
| Test a candidate | Number.isSafeInteger(n) |
| Larger exact ints | BigInt / 123n |
| Largest finite float | Number.MAX_VALUE (not exact ints) |
| Link to EPSILON | MAX_SAFE_INTEGER * EPSILON ≈ 2 |
Four facts to remember about Number.MAX_SAFE_INTEGER.
staticProperty on Number
2**53-19007199254740991
exactInteger precision holds
noRead-only constant
MAX_SAFE_INTEGER vs MAX_VALUE vs BigIntMAX_SAFE_INTEGER | MAX_VALUE | BigInt | |
|---|---|---|---|
| Approx. size | ~9.0e15 | ~1.8e308 | Arbitrary |
| Exact integers? | Yes (up to bound) | No (sparse) | Yes |
| Type | number | number | bigint |
| Use for | IDs / counts in range | Magnitude ceiling | Huge exact ints |
Examples follow MDN Number.MAX_SAFE_INTEGER patterns. Use View Output or Try It Yourself for each case.
Read the constant and see why “safe” matters.
Number.MAX_SAFE_INTEGERPrint the largest exact integer Number.
Number.MAX_SAFE_INTEGER;
// 9007199254740991 Same as 2 ** 53 - 1. Prefer the named constant in readable code.
+1 === +2 TrapPast the safe limit, consecutive integers are no longer distinct.
const x = Number.MAX_SAFE_INTEGER + 1;
const y = Number.MAX_SAFE_INTEGER + 2;
Number.MAX_SAFE_INTEGER; // 9007199254740991
x; // 9007199254740992
x === y; // true (!) Both x and y round to the same float. Math says they differ; IEEE-754 cannot tell them apart.
Checks, mantissa links, and magnitude vs precision.
isSafeInteger()Test whether a value is still inside the exact-integer window.
Number.isSafeInteger(Number.MAX_SAFE_INTEGER); // true
Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1); // false See the full isSafeInteger() tutorial for coercion rules and edge cases.
EPSILONBoth constants come from the 53-bit significand.
Number.MAX_SAFE_INTEGER * Number.EPSILON;
// 1.9999999999999998 (very close to 2) EPSILON is 2−52; MAX_SAFE_INTEGER is 253 − 1. Their product is almost 2 — an MDN teaching link between the two constants.
MAX_VALUEHuge finite floats are not the same as exact integers.
Number.MAX_VALUE > Number.MAX_SAFE_INTEGER; // true
Number.isSafeInteger(Number.MAX_VALUE); // false MAX_VALUE answers “how large?” MAX_SAFE_INTEGER answers “how exact for integers?”
MAX_SAFE_INTEGER or use strings.Number.isSafeInteger(n) before trusting integer math.MAX_VALUE for overflow-to-Infinity.Doubles store about 53 bits of precision for the fraction.
Every integer in that window has a unique Number encoding.
Some integers cannot be represented; neighbors collapse together.
Stay in range, or move to BigInt / string IDs.
Number.MAX_SAFE_INTEGER only.Number.MAX_SAFE_INTEGER() — it is not a function.Number.MIN_SAFE_INTEGER (-(253 − 1)).Number.MAX_SAFE_INTEGER is Baseline Widely available — part of ES2015 and supported in every modern browser and Node.js.
Safe for production everywhere. Use it as the named ceiling for exact Number integers.
Bottom line: Stay within MAX_SAFE_INTEGER for exact Number integers. Use BigInt past that. Use MAX_VALUE only for magnitude / Infinity overflow.
Number.MAX_SAFE_INTEGER is the static constant for the largest integer a JavaScript Number can represent exactly. Past that point, prefer BigInt (or strings) when every integer must stay distinct.
Continue with isSafeInteger(), MAX_VALUE, EPSILON, or the Number properties hub.
Number.MAX_SAFE_INTEGER on the constructorNumber.isSafeInteger(n)MIN_SAFE_INTEGER(5).MAX_SAFE_INTEGER or call it as a functionMAX_VALUE integers are exact===Number.MAX_SAFE_INTEGERStatic ceiling for exact Number integers.
static
API2**53-1
Constant+1 === +2
PitfallBigInt
Nextno
ReadonlyMany social / database “snowflake” IDs are 64-bit integers — larger than Number.MAX_SAFE_INTEGER. Storing them as plain JSON numbers can silently corrupt the last digits. Teams often keep them as strings or BigInt instead.
Return to the hub for static Number constants.
8 people found this page helpful