Maximum and Minimum Values for Integers in JavaScript

Introduction

JavaScript is a remarkably flexible language, but it's not without its limitations. One of these limitations is the maximum and minimum integer value that can be accurately represented. In this Byte, we'll discuss the JavaScript number type, the maximum and minimum integer values in JavaScript, and how you can work around these limitations if needed.

JavaScript's Number Type

In JavaScript, numbers are stored as 64-bit floating point values, following the international IEEE 754 standard. This format stores numbers in three parts: the sign, the exponent, and the fraction. It's this format that allows JavaScript to handle a very wide range of values, from very small to very large.

However, because of the way numbers are stored, there is a limit to the size of an integer that can be accurately represented.

Maximum Integer Value in JavaScript

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.

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 Number.MAX_SAFE_INTEGER.

Minimum Integer Value in JavaScript

Similarly, the smallest exact integral value that JavaScript can represent is -(2^53 - 1), or -9007199254740991. This value is defined in JavaScript as Number.MIN_SAFE_INTEGER.

console.log(Number.MIN_SAFE_INTEGER);
// Output: -9007199254740991

Remember that these limitations are inherent to the way JavaScript (and many other languages) represent numbers. If you need to work with larger or smaller integers, you'll need to use a workaround, like the BigInt type, which we'll briefly cover later in this Byte.

Other Number Properties in JavaScript

JavaScript provides a few other built-in properties to work with numbers. For instance, 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.

Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

console.log(Number.MAX_VALUE);  // 1.7976931348623157e+308
console.log(Number.MIN_VALUE);  // 5e-324

Note: Number.MIN_VALUE is not the lowest number JavaScript can handle. It's the smallest positive number greater than zero that JavaScript can represent with precision.

There's also Number.POSITIVE_INFINITY and 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 Number.MAX_SAFE_INTEGER, JavaScript has another number type called 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 BigInt function.

let big = 9007199254740993n;
console.log(big);  // 9007199254740993n

big = BigInt(9007199254740993);
console.log(big);  // 9007199254740993n

Note: BigInt and 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.

Conclusion

Understanding JavaScript's number properties, including the maximum and minimum integer values that can be accurately represented, is important for avoiding unexpected results in your calculations. For handling larger integers, BigInt is a great, built-in alternative.

Last Updated: September 13th, 2023
Was this helpful?

You might also like...

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms