Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
Number.MAX_SAFE_INTEGER
Number.MIN_SAFE_INTEGER

Try it

JS The edge of exact integers

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Detecting trouble and using BigInt

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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 n and n + 1 are different numbers. Above MAX_SAFE_INTEGER, whole numbers such as 2 ** 53 are representable, but 2 ** 53 + 1 rounds to the same value as 2 ** 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 length are limited to 2^32 - 1, not to the safe range.
  • Fractions have a similar issue at their own scale; see Number.EPSILON.
  • Mixing BigInt and number in arithmetic throws a TypeError; convert explicitly.

Browser support

Supported in all current browsers; it arrived with ES2015.