Categories
browser cross-browser javascript math

What is JavaScript’s highest integer value that a number can go to without losing precision?

1063

Is this defined by the language? Is there a defined maximum? Is it different in different browsers?

11

  • 6

    You don’t need to depend on JS’s limits with libraries like github.com/MikeMcl/big.js, see e.g. here for its reliability tests

    May 18, 2016 at 4:26

  • 4

    what’s the highest integer value you can use with big.js ?

    – George

    Mar 26, 2018 at 20:41

  • 1

    @DmitriZaitsev We don’t need to depend on external libraries any more (on some browsers, at least). 1n << 10000n is a really, really big integer, without losing any precision, without requiring any dependencies (and needless to say, not even close to a limit).

    – Amadan

    Jan 6, 2020 at 9:12


  • 1

    @DmitriZaitsev Notice the n suffix. BigInt class is a part of ES2020 spec draft, already implemented in the majority of browsers; you can try to evaluate that in e.g. Chrome or Firefox, with no external libraries, and get a 3011-digit BigInt.

    – Amadan

    Jan 8, 2020 at 11:41


  • 2

    @DmitriZaitsev: Yes, it is only for integers. This question is about integers.

    – Amadan

    Jan 8, 2020 at 12:01

953

JavaScript has two number types: Number and BigInt.

The most frequently-used number type, Number, is a 64-bit floating point IEEE 754 number.

The largest exact integral value of this type is Number.MAX_SAFE_INTEGER, which is:

  • 253-1, or
  • +/- 9,007,199,254,740,991, or
  • nine quadrillion seven trillion one hundred ninety-nine billion two hundred fifty-four million seven hundred forty thousand nine hundred ninety-one

To put this in perspective: one quadrillion bytes is a petabyte (or one thousand terabytes).

“Safe” in this context refers to the ability to represent integers exactly and to correctly compare them.

From the spec:

Note that all the positive and negative integers whose magnitude is no
greater than 253 are representable in the Number type (indeed, the
integer 0 has two representations, +0 and -0).

To safely use integers larger than this, you need to use BigInt, which has no upper bound.

Note that the bitwise operators and shift operators operate on 32-bit integers, so in that case, the max safe integer is 231-1, or 2,147,483,647.

const log = console.log
var x = 9007199254740992
var y = -x
log(x == x + 1) // true !
log(y == y - 1) // also true !

// Arithmetic operators work, but bitwise/shifts only operate on int32:
log(x / 2)      // 4503599627370496
log(x >> 1)     // 0
log(x | 1)      // 1

Technical note on the subject of the number 9,007,199,254,740,992: There is an exact IEEE-754 representation of this value, and you can assign and read this value from a variable, so for very carefully chosen applications in the domain of integers less than or equal to this value, you could treat this as a maximum value.

In the general case, you must treat this IEEE-754 value as inexact, because it is ambiguous whether it is encoding the logical value 9,007,199,254,740,992 or 9,007,199,254,740,993.

27

  • 76

    This seems right, but is there someplace where this is defined, á la C’s MAX_INT or Java’s Integer.MAX_VALUE?

    – TALlama

    Nov 20, 2008 at 23:35

  • 51

    4294967295 === Math.pow(2,32) - 1;

    – coolaj86

    Aug 23, 2011 at 20:15


  • 13

    So what’s the smallest and largest integer we can use to assure exact precision?

    – Pacerier

    Oct 15, 2011 at 16:21

  • 40

    Maybe worth noting that there is no actual (int) in javascript. Every instance of Number is (float) or NaN.

    Aug 31, 2012 at 13:09


  • 58

    9007199254740992 is not really the maximum value, the last bit here is already assumed to be zero and so you have lost 1 bit of precision. The real safe number is 9007199254740991 ( Number.MAX_SAFE_INTEGER )

    Aug 21, 2014 at 17:59

485

>= ES6:

Number.MIN_SAFE_INTEGER;
Number.MAX_SAFE_INTEGER;

<= ES5

From the reference:

Number.MAX_VALUE;
Number.MIN_VALUE;

console.log('MIN_VALUE', Number.MIN_VALUE);
console.log('MAX_VALUE', Number.MAX_VALUE);

console.log('MIN_SAFE_INTEGER', Number.MIN_SAFE_INTEGER); //ES6
console.log('MAX_SAFE_INTEGER', Number.MAX_SAFE_INTEGER); //ES6

10

  • 23

    I’ve edited the question to be a bit more precise about wanting the max Integer values, not just the max Number value. Sorry for the confusion, here.

    – TALlama

    Nov 20, 2008 at 23:21

  • 5

    Is the returned result guaranteed to be equal on all browsers?

    – Pacerier

    Sep 21, 2013 at 19:05


  • 7

    Note that Number.MIN_VALUE is the smallest possible positive number. The least value (i.e. less than anything else) is probably -Number.MAX_VALUE.

    Jun 10, 2014 at 23:19

  • 2

    This is the maximum floating point value. The question is about the highest integer value. And while Number.MAX_VALUE is an integer, you can’t go past 2^53 without losing precision.

    – Teepeemm

    Jul 22, 2014 at 22:01

  • 34

    ES6 introduces Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER

    Aug 31, 2014 at 15:23

118

It is 253 == 9 007 199 254 740 992. This is because Numbers are stored as floating-point in a 52-bit mantissa.

The min value is -253.

This makes some fun things happening

Math.pow(2, 53) == Math.pow(2, 53) + 1
>> true

And can also be dangerous 🙂

var MAX_INT = Math.pow(2, 53); // 9 007 199 254 740 992
for (var i = MAX_INT; i < MAX_INT + 2; ++i) {
    // infinite loop
}

Further reading: http://blog.vjeux.com/2010/javascript/javascript-max_int-number-limits.html

4

  • 1

    though one would never reach the end of that for loop in a sane timeframe, you may wish to say i += 1000000000

    Jul 8, 2015 at 20:18

  • 3

    @ninjagecko, he starts at MAX_INT so the end is right there. Also using i+= 1000000000 would make it no longer an infinite loop. Try it.

    Jan 5, 2016 at 0:52

  • @TedBigham: Ah oops, was ready too quickly through that. Thanks for correcting me twice.

    Jan 5, 2016 at 8:34

  • See Jimmy’s argument for 9,007,199,254,740,991 instead of 9,007,199,254,740,992 here. That, combined with my follow-up, seems persuasive.

    Sep 29, 2018 at 17:19