# What is the greatest integer value a number can reach in JavaScript without losing precision?

## Problem

Is the language defining this? Is there a certain upper limit? Is it different depending on the browser?

## Solution #1

Number and BigInt are the two number types in JavaScript.

Number is a 64-bit floating point IEEE 754 number, which is the most commonly used number type.

Number.MAX SAFE INTEGER has the maximum precise integral value of this type, which is:

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

In this context, “safe” refers to the capacity to accurately represent and compare integers.

From the spec:

You must use BigInt, which has no upper bound, to safely use numbers larger than this.

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.

A technical comment on the number 9,007,199,254,740,992 is as follows: This value has an accurate IEEE-754 representation, and you can assign and read it from a variable, so you may treat it as a maximum value for very carefully chosen applications in the domain of integers less than or equal to this value.

Because it’s unclear if this IEEE-754 value is encoding the logical value 9,007,199,254,740,992 or 9,007,199,254,740,993, you should treat it as inexact in most cases.

## Solution #2

>= ES6:

``````Number.MIN_SAFE_INTEGER;
Number.MAX_SAFE_INTEGER;
``````

<= ES5

From the reference:

``````Number.MAX_VALUE;
Number.MIN_VALUE;
``````

## Solution #3

253 == 9 007 199 254 740 992 is the number. Because numbers are stored as floating-point in a 52-bit mantissa, this is the case.

-253 is the minimum value.

As a result, some interesting things occur.

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

And it’s also potentially 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

There is a number named Infinity in JavaScript.

Examples:

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

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

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

For certain questions on this subject, this may be adequate.