However, because of the way numbers are stored, there is a limit to the size of an integer that can be accurately represented.
console.log(Number.MAX_SAFE_INTEGER); // Output: 9007199254740991
Note: The term "safe" in
MAX_SAFE_INTEGER refers to the ability to represent integers exactly and to correctly compare them. For example,
(Number.MAX_SAFE_INTEGER + 1) === Number.MAX_SAFE_INTEGER will return
true, because the value of
Number.MAX_SAFE_INTEGER + 1 can't be represented exactly and is rounded to
console.log(Number.MIN_SAFE_INTEGER); // Output: -9007199254740991
BigInt type, which we'll briefly cover later in this Byte.
Number.MAX_VALUE returns the largest positive representable number, which is approximately 1.8e+308. On the other hand,
Number.MIN_VALUE gives the smallest positive representable number, which is about 5e-324.
console.log(Number.MAX_VALUE); // 1.7976931348623157e+308 console.log(Number.MIN_VALUE); // 5e-324
Number.NEGATIVE_INFINITY which represent the mathematical concepts of positive and negative infinity.
console.log(Number.POSITIVE_INFINITY); // Infinity console.log(Number.NEGATIVE_INFINITY); // -Infinity
BigInt: An Alternative for Larger Integers
When dealing with numbers larger than
BigInt. It can represent integers of arbitrary length. A
BigInt is created by appending
n to the end of an integer or by calling the
let big = 9007199254740993n; console.log(big); // 9007199254740993n big = BigInt(9007199254740993); console.log(big); // 9007199254740993n
Number are not implicitly convertible. You have to manually convert them if you want to use them together in an operation. Keep in mind that you may not be able to accuratley convert a
BigInt to a
Number due to the fact that it can represent much larger numbers than
Number is able to handle.
BigInt is a great, built-in alternative.