Is this defined by the language? Is there a defined maximum? Is it different in different browsers?
Javascript – JavaScript’s highest integer value that a number can go to without losing precision
browsercross-browserjavascriptmath
Related Topic
- Javascript – var functionName = function() {} vs function functionName() {}
- Javascript – How to copy to the clipboard in JavaScript
- The maximum length of a URL in different browsers
- Css – How to disable text selection highlighting
- Javascript – What does “use strict” do in JavaScript, and what is the reasoning behind it
- Javascript – How to check whether a string contains a substring in JavaScript
- Javascript – How to remove a specific item from an array
- Javascript – Why does the JavaScript code receive a “No ‘Access-Control-Allow-Origin’ header is present on the requested resource” error, while Postman does not
Best Answer
JavaScript has two number types:
Number
andBigInt
.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: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:
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.
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.