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

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

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

### Solution 1:

+/- 9007199254740991

ECMA Section 8.5 – Numbers

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).

They are 64-bit floating point values, the largest exact integral value is 253-1, or `9007199254740991`. In ES6, this is defined as Number.MAX_SAFE_INTEGER.

Note that the bitwise operators and shift operators operate on 32-bit ints, so in that case, the max safe integer is 231-1, or 2147483647.

Test it out!

``````var x = 9007199254740992;
var y = -x;
x == x + 1; // true !
y == y - 1; // also true !
// Arithmetic operators work, but bitwise/shifts only operate on int32:
x / 2;      // 4503599627370496
x >> 1;     // 0
x | 1;      // 1
``````

Technical note on the subject of the number 9007199254740992: 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 9007199254740992 or 9007199254740993.

### Solution 2:

>= 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``````

### Solution 3:

It is 253 == 9 007 199 254 740 992. This is because `Number`s 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
}
``````

### Solution 4:

In JavaScript, there is a number called `Infinity`.

Examples:

``````(Infinity>100)
=> true

// Also worth noting
Infinity - 1 == Infinity
=> true

Math.pow(2,1024) === Infinity
=> true
``````

This may be sufficient for some questions regarding this topic.

### Solution 5:

Jimmy’s answer correctly represents the continuous JavaScript integer spectrum as -9007199254740992 to 9007199254740992 inclusive (sorry 9007199254740993, you might think you are 9007199254740993, but you are wrong!
Demonstration below or in jsfiddle).

``document.write(9007199254740993);``

However, there is no answer that finds/proves this programatically (other than the one CoolAJ86 alluded to in his answer that would finish in 28.56 years ;), so here’s a slightly more efficient way to do that (to be precise, it’s more efficient by about 28.559999999968312 years :), along with a test fiddle:

``````/**
* Checks if adding/subtracting one to/from a number yields the correct result.
*
* @param number The number to test
* @return true if you can add/subtract 1, false otherwise.
*/
var numMinusOne = number - 1;
var numPlusOne = number + 1;

return ((number - numMinusOne) === 1) && ((number - numPlusOne) === -1);
}

//Find the highest number

//Get a number higher than the valid integer range
highestNumber *= 2;
}

//Find the lowest number you can't add/subtract 1 from
var numToSubtract = highestNumber / 4;
while (numToSubtract >= 1) {
highestNumber = highestNumber - numToSubtract;
}

numToSubtract /= 2;
}

//And there was much rejoicing.  Yay.
console.log('HighestNumber = ' + highestNumber);``````

# To be safe

``````var MAX_INT = 4294967295;
``````

# Reasoning

I thought I’d be clever and find the value at which `x + 1 === x` with a more pragmatic approach.

My machine can only count 10 million per second or so… so I’ll post back with the definitive answer in 28.56 years.

If you can’t wait that long, I’m willing to bet that

• Most of your loops don’t run for 28.56 years
• `9007199254740992 === Math.pow(2, 53) + 1` is proof enough
• You should stick to `4294967295` which is `Math.pow(2,32) - 1` as to avoid expected issues with bit-shifting

Finding `x + 1 === x`:

``````(function () {
"use strict";

var x = 0
, start = new Date().valueOf()
;

while (x + 1 != x) {
if (!(x % 10000000)) {
console.log(x);
}

x += 1
}

console.log(x, new Date().valueOf() - start);
}());
``````