Is this defined by the language? Is there a defined maximum? Is it different in different browsers?
11
JavaScript has two number types: Number
and BigInt
.
The most frequentlyused number type, Number
, is a 64bit floating point IEEE 754 number.
The largest exact integral value of this type is Number.MAX_SAFE_INTEGER
, which is:
 2^{53}1, or
 +/ 9,007,199,254,740,991, or
 nine quadrillion seven trillion one hundred ninetynine billion two hundred fiftyfour million seven hundred forty thousand nine hundred ninetyone
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.
Note that all the positive and negative integers whose magnitude is no
greater than 2^{53} are representable in theNumber
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 32bit integers, so in that case, the max safe integer is 2^{31}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 IEEE754 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 IEEE754 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?
– TALlamaNov 20, 2008 at 23:35
 51
 13
So what’s the smallest and largest integer we can use to assure exact precision?
– PacerierOct 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
>= 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.
– TALlamaNov 20, 2008 at 23:21
 5
 7
Note that
Number.MIN_VALUE
is the smallest possible positive number. The least value (i.e. less than anything else) is probablyNumber.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 past2^53
without losing precision.– TeepeemmJul 22, 2014 at 22:01
 34
ES6 introduces
Number.MIN_SAFE_INTEGER
andNumber.MAX_SAFE_INTEGER
Aug 31, 2014 at 15:23
It is 2^{53} == 9 007 199 254 740 992. This is because Number
s are stored as floatingpoint in a 52bit mantissa.
The min value is 2^{53}.
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/javascriptmax_intnumberlimits.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 followup, seems persuasive.
Sep 29, 2018 at 17:19
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
what’s the highest integer value you can use with big.js ?
Mar 26, 2018 at 20:41
@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).Jan 6, 2020 at 9:12
@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 3011digitBigInt
.Jan 8, 2020 at 11:41
@DmitriZaitsev: Yes, it is only for integers. This question is about integers.
Jan 8, 2020 at 12:01

Show 6 more comments