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-precision floating-point format numbers as specified in IEEE 754.

Number and BigInt are the two number types available in JavaScript.

Number, the most popular type of number, is a 64-bit floating point IEEE 754 number.

Number.MAX SAFE INTEGER, which is the following, has the largest precise integral value of this type −

  • 253-1, or

  • +/- 9,007,199,254,740,991, or

  • In terms of numbers, there are nine quadrillion seven trillion one hundred ninety-nine billion two hundred fifty-four million seven hundred forty thousand nine hundred ninety-one

    So put that in context, a petabyte is one quadrillion bytes (or one thousand terabytes). In this context, the term "safe" refers to the capacity to accurately represent and compare integers. You must use BigInt, which has no upper bound, to securely use integers larger than this.

    The maximum safe integer in that situation is 231-1, or 2,147,483,647. This is because bitwise and shift operators only work on 32-bit numbers.

Example 1

In this example let us understand how 32-bit integers can be used with the bitwise and shift operators.

<!DOCTYPE html> <html> <title>What is JavaScript’s highest integer value that a Number can go to without losing precision? - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> const log = console.log let a = 9007199254740992 let b = -a log(a == a + 1) // true ! log(b == b - 1) // this is also true ! // Bitwise/shifts are only used on int32, although arithmetic operators are acceptable. log(a / 2) // 4503599627370496 log(a >> 1) // 0 log(a | 1) // 1 </script> </body> </html>

Please press the f12 key on your keyboard to access the browser console to see the results.

Example 2

The number. The largest safe integer that you can use in Typescript is represented by the MAX SAFE INTEGER constant. It is the Number object's static property. Its worth is 9007199254740991.

The term "safe" refers to the fact that any number greater than the one mentioned above cannot be assumed to be accurately and appropriately represented. This restriction is imposed by the doubleprecision 64-bit number format used by Javascript rather than Typescript.

<!DOCTYPE html> <html> <title>What is JavaScript’s highest integer value that a Number can go to without losing precision? - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> document.write(Number.MAX_SAFE_INTEGER +'<br>') //9007199254740991 document.write(Number.MIN_SAFE_INTEGER) //-9007199254740991 </script> </body> </html>

Example 3

In this example let us understand how the Integer.isSafeInteger method can be used to verify whether a number is safe.

<!DOCTYPE html> <html> <title>What is JavaScript’s highest integer value that a Number can go to without losing precision? - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> document.write(Number.isSafeInteger(Number.MAX_SAFE_INTEGER) +'<br>'); //this returns true document.write(Number.isSafeInteger(Number.MAX_SAFE_INTEGER+1)); //this returns false </script> </body> </html>

MAX_VALUE & MIN_VALUE

Example 4

The number 1.7976931348623157e+308 is the greatest number that the number data type can represent. MAX VALUE. Number.MIN VALUE is the smallest number.

<!DOCTYPE html> <html> <title>What is JavaScript’s highest integer value that a Number can go to without losing precision? - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> document.write(Number.MAX_VALUE +'<br>') document.write(Number.MIN_VALUE) </script> </body> </html>

Example 5

Numbers exceed Number. The double-precision 64-bit binary system is unable to represent MAX VALUE. Therefore, whatever value above the MAX VALUE is trimmed to the MAX VALUE. Whereas an extremely large value delivers an infinite value.

<!DOCTYPE html> <html> <title>What is JavaScript’s highest integer value that a Number can go to without losing precision? - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> document.write(Number.MAX_VALUE + 100 == Number.MAX_VALUE); //true document.write("<br>"); document.write(Number.MAX_VALUE + Number.MAX_VALUE); //Infinity </script> </body> </html>

In Brief

The static properties of the Typescript Number object are MAX SAFE INTEGER, MIN SAFE INTEGER, MAX VALUE, and MIN VALUE. The Maximum/Minimum values that the number data type can support are represented by these. It is essential to use the MAX SAFE INTEGER and MIN SAFE INTEGER because any integer higher than these two is not assured to be represented accurately and correctly.

Updated on: 24-Aug-2022

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements