Number Constants: MIN_SAFE_INTEGER, MAX_SAFE_INTEGER
What you'll learn: How to reference the exact boundaries of JavaScript's safe integer range using built-in constants.
The Safe Integer Boundaries
In the previous lesson, you learned that JavaScript can safely represent integers between -(2⁵³ - 1) and (2⁵³ - 1). But who wants to remember those formulas? JavaScript provides two handy constants that give you these exact values: Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER.
Think of these constants as the official "guardrails" of the safe integer highway. They tell you precisely where the safe zone begins and ends.
Using the Constants
console.log(Number.MAX_SAFE_INTEGER);
// 9007199254740991
console.log(Number.MIN_SAFE_INTEGER);
// -9007199254740991
// Check if a number is within safe bounds
const bigNumber = 9007199254740991;
console.log(bigNumber <= Number.MAX_SAFE_INTEGER);
// true
const tooBig = 9007199254740992;
console.log(tooBig <= Number.MAX_SAFE_INTEGER);
// false
Practical Use Cases
These constants are perfect for validation logic:
const userInput = 9000000000000000;
if (userInput >= Number.MIN_SAFE_INTEGER &&
userInput <= Number.MAX_SAFE_INTEGER) {
console.log("Safe to use!");
} else {
console.log("Number too large for precise arithmetic");
}
You can also combine them with Number.isSafeInteger() for cleaner code, but the constants themselves let you create custom boundary checks when needed.
Key Takeaway: Use Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER to reference the exact safe integer boundaries (-9007199254740991 to 9007199254740991) without memorizing the formula.