JavaScript Reference ยท Number and BigInt
Number.MAX_SAFE_INTEGER
Reference for Number.MAX_SAFE_INTEGER and MIN_SAFE_INTEGER: the 2^53 - 1 limit of exact integers in JavaScript, what breaks beyond it, and BigInt as the fix.
Number.MAX_SAFE_INTEGER is the largest integer that a JavaScript number can represent exactly and that no other integer rounds to: 9007199254740991, which is 2^53 - 1. Its counterpart Number.MIN_SAFE_INTEGER is -9007199254740991.
Beyond that limit, numbers are still valid, but not every integer can be stored, so arithmetic can silently give wrong answers. Use it to guard IDs, counters and other values that must stay exact.
Number.MAX_SAFE_INTEGER Number.MIN_SAFE_INTEGER
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Number.MAX_SAFE_INTEGER // 9007199254740991 Number.MIN_SAFE_INTEGER // -9007199254740991
| Part | Description |
|---|---|
| Value | 2 ** 53 - 1 and -(2 ** 53 - 1). |
| Attributes | Not writable, not enumerable, not configurable. |
Usage notes
- "Safe" means two things: the integer is exactly representable, and
nandn + 1are different numbers. AboveMAX_SAFE_INTEGER, whole numbers such as2 ** 53are representable, but2 ** 53 + 1rounds to the same value as2 ** 53. - Check with
Number.isSafeInteger()instead of comparing against the constants by hand. - Large IDs from databases or APIs (Twitter-style snowflake IDs, 64-bit integers) often exceed the limit. Transfer them as strings, or parse them to
BigInt. JSON.parse()cannot return BigInt; it rounds big numbers to doubles. Keep such fields as strings in your JSON.- Bitwise operators work on 32-bit integers, which is a much smaller range (about +/- 2.1 billion).
- Array indexes and
lengthare limited to 2^32 - 1, not to the safe range. - Fractions have a similar issue at their own scale; see
Number.EPSILON. - Mixing
BigIntandnumberin arithmetic throws aTypeError; convert explicitly.
Browser support
Supported in all current browsers; it arrived with ES2015.