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.

Answer from Jimmy on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › MAX_SAFE_INTEGER
Number.MAX_SAFE_INTEGER - JavaScript | MDN
Number.MAX_SAFE_INTEGER represents ... The largest representable number in JavaScript is actually Number.MAX_VALUE, which is approximately 1.7976931348623157 × 10308....
Top answer
1 of 16
1011

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.

2 of 16
509

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

Top answer
1 of 3
75
Fun question! So in JavaScript, every value with the "number" type is represented under the hood with a 64-bit (or "double-precision") floating point number. Floating point numbers represent a value with a combination of fractions and exponents, which allows them to represent a lot of numbers (with varying degrees of precision), but at the end of the day, the value still has to fit within 64-bits somehow. So there are limits. In this case, the highest number a 64-bit float can possibly represent happens to be 1.7976931348623157e+308 (this number is stored in the constant Number.MAX_VALUE should you ever need to reference it). Your value of 1e+309 is larger than that, so the best a 64-bit float can do is call it Infinity. Under the IEEE 754 floating-point standard (which JavaScript uses), there are some special values, like NaN and Infinity. You can basically think of NaN as "something went wrong with this number" and Infinity as "this number is bigger than I can do anything useful with". Both special values sort of infect anything they touch. The result of any math with NaN is NaN. The result of (almost) any math with Infinity is Infinity. console.log(NaN * 2); // NaN console.log(Infinity - 100); // Infinity console.log(Infinity * 0); // NaN Jumping back to those varying degrees of precision, the highest integer you can represent without losing any precision happens to be a good deal smaller than 1e+308. Since everything is actually a float under the hood, you can only get up to 253 - 1 (or 9,007,199,254,740,991) before you can't trust integer math to work right anymore (that number is stored in the constant Number.MAX_SAFE_INTEGER by the way). console.log(9007199254740991 + 10); // 9007199254741000 Now, the good news for anyone who loves big numbers, is that JavaScript recently added a new primitive type, "bigint". You can make a value a BigInt by adding an n to the end, or by calling BigInt on it. const num = 7; // <-- number const big = 7n; // <-- bigint const int = BigInt(7); // <-- bigint There are two big differences between BigInts and vanilla numbers. They are integers, not floating point. This means you can't represent fractions, but you also never lose precision. They are variable-bit. They start as 64-bit, but if the value gets to large, more bits are added. So with BigInt, we can represent big numbers. let x = 10n ** 309n; // e-notation not supported with BigInt console.log(x); // 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000n And we can safely do big integer math. console.log(9007199254740991n + 10n); // 9007199254741001n But any decimal places just end up getting dropped. console.log(5n / 2n); // 2n
2 of 3
9
JavaScript uses double-precision floating point values to store numbers. The highest number you can store in this format is 1.7976931348623157e+308 or 2^1024. Anything higher than that is rounded to Infinity, as part of the floating point spec.
🌐
W3Schools
w3schools.com › jsref › jsref_max_value.asp
JavaScript MAX_VALUE Property
Number.MAX_VALUE returns the largest number possible in JavaScript. Number.MAX_VALUE has the value of 1.7976931348623157e+308. Numbers larger than MAX_VALUE are represented as Infinity.
🌐
Stack Abuse
stackabuse.com › bytes › maximum-and-minimum-values-for-integers-in-javascript
Maximum and Minimum Values for Integers in JavaScript
September 13, 2023 - The largest exact integral value that JavaScript can represent is 2^53 - 1, or 9007199254740991. This value is defined in JavaScript as Number.MAX_SAFE_INTEGER.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Number › MAX_SAFE_INTEGER
JavaScript Number MAX_SAFE_INTEGER - Maximum Safe Integer | Vultr Docs
December 2, 2024 - In JavaScript, Number.MAX_SAFE_INTEGER represents the highest safe integer value that can be precisely represented in JavaScript.
🌐
TutorialsPoint
tutorialspoint.com › 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?
The value of the MAX SAFE INTEGER constant is 9007199254740991 (9,007,199,254,740,991 or nine quadrillion). JavaScript only properly represents integers between -(253 - 1) and 253 - 1, which is the rationale for that number. JavaScript employs double
Find elsewhere
🌐
Programiz
programiz.com › javascript › library › number › max_safe_integer
JavaScript Number.MAX_SAFE_INTEGER
Become a certified JavaScript programmer. Try Programiz PRO! ... The MAX_SAFE_INTEGER constant has a value of 253 - 1 (9007199254740991).
🌐
TechOnTheNet
techonthenet.com › js › number_max_value.php
JavaScript: Number.MAX_VALUE property
The MAX_VALUE property returns the maximum numeric value that can be represented in JavaScript which is 1.7976931348623157e+308.
🌐
TechOnTheNet
techonthenet.com › js › number_max_safe_integer.php
JavaScript: Number.MAX_SAFE_INTEGER property
In JavaScript, the syntax for the MAX_SAFE_INTEGER property is: ... There are no parameters or arguments for the MAX_SAFE_INTEGER property. The MAX_SAFE_INTEGER property returns the maximum safe integer value of 9007199254740991 which can be represented as 253 - 1.
🌐
Vjeux
blog.vjeux.com › 2010 › javascript › javascript-max_int-number-limits.html
Vjeux » Javascript – MAX_INT: Number Limits
You should take care using traditional loops with big numbers, they will go infinite after that maximum integer value. // WARNING: DO NOT TRY THIS AT HOME! 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 } The integer part ...
🌐
Programiz
programiz.com › javascript › library › number › max_value
JavaScript Number.MAX_VALUE
JavaScript Number.MAX_SAFE_INTEGER · The MAX_VALUE property has a value of approximately 1.79E+308 or 21024. Values larger than MAX_VALUE are represented as Infinity. It is a non-writable, non-enumerable, and non-configurable property. The syntax to access the MAX_VALUE constant is: ...
Top answer
1 of 3
12

This is not a strongly typed programming language. JS has an object Number. You can even get an infinite number: document.write(Math.exp(1000));.

document.write(Number.MIN_VALUE + "<br>");
document.write(Number.MAX_VALUE + "<br>");

document.write(Number.POSITIVE_INFINITY + "<br>");
document.write(Number.NEGATIVE_INFINITY + "<br>");

    alert([
         Number.MAX_VALUE/(1e293),
         Number.MAX_VALUE/(1e292),
         Number.MAX_VALUE/(1e291),
         Number.MAX_VALUE/(1e290),
    ].join('\n'))

Hope it's a useful answer. Thanks!

UPDATE: max int is - +/- 9007199254740992

2 of 3
8

You can find some information on JavaScript's Number type here: ECMA-262 5th Edition: The Number Type.

As it mentions, numbers are represented as a 64-bit floating-point number, with 53 bits of mantissa (significant digits) and 11 bits for the exponent (IEEE 754). The result is then obtained with: mantissa * 2^exponent.

This means that up to 2^53 values can be represented in the mantissa (of those a few numbers have special meanings, and the others are positive and negative integers).

The number 2^53 (9007199254740992) can't be represented in the mantissa, and you have to use an exponent. As an example, you can represent 2^53 as (9007199254740992 / 2) * 2^1, ie. mantissa = 9007199254740992 / 2 = 4503599627370496 and exponent = 1.

Let's check what happens with 2^53+1 (9007199254740993). Here we have to do the same, mantissa = 9007199254740993 / 2 = 4503599627370496. Oops, isn't this the same mantissa we had for 2^53? Looks like there's been some rounding error! :)

(Note: the above examples are not actually how it really works: the mantissa is always interpreted as having a dot after the first digit, which means that eg. the number 3 is actually stored as 1.5*2. I omitted this in the above explanation to make it easier to follow.)

You can find some more information on floating-point numbers (in general) here: What Every Computer Scientist Should Know About Floating-Point Arithmetic.

🌐
W3Schools
w3schools.com › jsref › jsref_max.asp
JavaScript Math max() Method
❮ Previous JavaScript Math Object ... -10); let e = Math.max(1.5, 2.5); Try it Yourself » · The Math.max() method returns the number with the highest value....
🌐
W3Schools
w3schools.com › jsref › jsref_max_safe_integer.asp
JavaScript MAX_SAFE_INTEGER Property
Number.MAX_SAFE_INTEGER represents the maximum safe integer in JavaScript.