Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Integer Range in JavaScript
JavaScript uses a single number type to represent all numeric values, including both integers and floating-point numbers. Unlike languages such as C++ or Java, JavaScript doesn't have separate data types for different number sizes. However, there are important limits to understand when working with large integers.
JavaScript stores numbers using the IEEE 754 double-precision floating-point format, which determines the maximum and minimum values that can be safely represented and manipulated.
JavaScript Number System
JavaScript treats all numbers as 64-bit floating-point values. This unified approach simplifies the language but introduces precision limitations when working with very large integers. There are two important ranges to consider: the absolute maximum values and the "safe" integer ranges for reliable calculations.
Maximum Number Value
The largest finite number that can be represented in JavaScript is approximately 1.79E+308. This value is accessible through Number.MAX_VALUE.
console.log("Maximum value:", Number.MAX_VALUE);
console.log("Scientific notation:", Number.MAX_VALUE.toExponential());
Maximum value: 1.7976931348623157e+308 Scientific notation: 1.80e+308
Minimum Positive Value
The smallest positive number greater than zero is approximately 5E-324, accessible through Number.MIN_VALUE.
console.log("Minimum positive value:", Number.MIN_VALUE);
console.log("Scientific notation:", Number.MIN_VALUE.toExponential());
Minimum positive value: 5e-324 Scientific notation: 5.00e-324
Safe Integer Limits
For integer operations, JavaScript provides "safe" limits where all integers can be represented exactly without precision loss.
Maximum Safe Integer
The largest integer that can be safely represented is 253 - 1 (9,007,199,254,740,991).
console.log("Maximum safe integer:", Number.MAX_SAFE_INTEGER);
console.log("Value:", Number.MAX_SAFE_INTEGER);
// Demonstrating precision loss beyond safe limit
console.log("Beyond safe limit:", Number.MAX_SAFE_INTEGER + 1);
console.log("Further beyond:", Number.MAX_SAFE_INTEGER + 2);
Maximum safe integer: 9007199254740991 Value: 9007199254740991 Beyond safe limit: 9007199254740992 Further beyond: 9007199254740992
Minimum Safe Integer
The smallest integer that can be safely represented is -(253 - 1).
console.log("Minimum safe integer:", Number.MIN_SAFE_INTEGER);
console.log("Value:", Number.MIN_SAFE_INTEGER);
Minimum safe integer: -9007199254740991 Value: -9007199254740991
Checking Safe Integers
JavaScript provides Number.isSafeInteger() to verify if a number is within the safe integer range.
console.log("Is safe:", Number.isSafeInteger(9007199254740991)); // true
console.log("Is safe:", Number.isSafeInteger(9007199254740992)); // false
console.log("Is safe:", Number.isSafeInteger(3.14)); // false
Is safe: true Is safe: false Is safe: false
Practical Implications
| Constant | Value | Use Case |
|---|---|---|
| Number.MAX_VALUE | ~1.8 × 10308 | Maximum representable number |
| Number.MIN_VALUE | ~5 × 10-324 | Smallest positive number |
| Number.MAX_SAFE_INTEGER | 253 - 1 | Safe integer calculations |
| Number.MIN_SAFE_INTEGER | -(253 - 1) | Safe negative integer calculations |
Conclusion
JavaScript's unified number system uses safe integer limits of ±(253 - 1) for reliable calculations. Beyond these limits, precision loss occurs, making Number.isSafeInteger() essential for validating large integer operations.
