By the end of this lesson, you can use JavaScript number properties such as Number.MAX_SAFE_INTEGER, Number.MAX_VALUE, and Number.EPSILON to reason about integer limits, finite range, and floating-point precision.
What it is
Number properties are static constants attached to the Number constructor. They describe limits and precision characteristics of JavaScript’s number system, which uses IEEE-754 double-precision floating-point values.
Number.MAX_SAFE_INTEGER is the largest integer that can be represented exactly in JavaScript: 2^53 - 1, or 9007199254740991. Integers above this value may lose precision because the floating-point format cannot represent every whole number exactly.
Number.MAX_VALUE is the largest finite number JavaScript can represent, approximately 1.7976931348623157e+308. It is not the same as the largest safe integer.
Number.EPSILON is the difference between 1 and the next representable number above 1. It is useful when comparing floating-point numbers because operations like 0.1 + 0.2 may not produce an exact result.
Related properties include Number.MIN_SAFE_INTEGER, Number.MIN_VALUE, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY, and Number.isSafeInteger().
Why it matters
- It helps you avoid silent integer overflow when working with IDs, counters, or timestamps.
- It clarifies the difference between “largest finite number” and “largest exact integer.”
- It supports safer floating-point comparisons using a small tolerance instead of strict equality.
- It helps you decide when to switch from
NumbertoBigInt. - It prevents bugs caused by assuming JavaScript can represent every large whole number exactly.
Syntax or steps
Access these constants directly from the Number object. Do not create a new Number instance to use them.
Number.MAX_SAFE_INTEGER;
Number.MAX_VALUE;
Number.EPSILON;
Number.isSafeInteger(value);
For integer safety, compare values against Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER, or use Number.isSafeInteger(). For floating-point equality, compare the absolute difference against a tolerance.
Example
const safeLimit = Number.MAX_SAFE_INTEGER;
const maxFinite = Number.MAX_VALUE;
const epsilon = Number.EPSILON;
console.log(safeLimit);
console.log(safeLimit + 1 === safeLimit + 2);
console.log(maxFinite * 2);
console.log(0.1 + 0.2 === 0.3);
console.log(Math.abs((0.1 + 0.2) - 0.3) < epsilon);
function nearlyEqual(a, b) {
return Math.abs(a - b) < epsilon;
}
console.log(nearlyEqual(0.1 + 0.2, 0.3));
safeLimitstores the largest safe integer.console.log(safeLimit)prints9007199254740991.safeLimit + 1 === safeLimit + 2printstruebecause numbers beyond the safe integer range can lose precision.maxFinite * 2printsInfinitybecause the result exceeds the largest finite number.0.1 + 0.2 === 0.3printsfalsedue to floating-point rounding.- The epsilon comparison prints
truebecause the tiny difference is smaller thanNumber.EPSILON.
Common mistakes
- Using
Number.MAX_VALUEto check integer safety. Fix: useNumber.MAX_SAFE_INTEGERorNumber.isSafeInteger(). - Comparing floating-point numbers with
===. Fix: compare the absolute difference against a tolerance. - Using
Number.EPSILONas a universal absolute tolerance. Fix: for very large or very small numbers, use a relative tolerance based on the magnitude of the values. - Assuming all large integers are safe. Fix: if you need exact arbitrary-size integers, use
BigInt.
When to use it
| Goal | Use | Why |
|---|---|---|
| Check the largest exact integer | Number.MAX_SAFE_INTEGER |
It marks the boundary where integer precision may fail. |
| Check the largest finite number | Number.MAX_VALUE |
It is about overflow to Infinity, not exact integer representation. |
| Compare floating-point results | Number.EPSILON |
It helps account for tiny rounding errors. |
| Work with very large exact integers | BigInt |
It supports integers beyond the safe Number range. |
Practice
Guided exercise: Write a function called isSafeInt(n) that returns true only when n is a safe integer.
Solution hint: Use Number.isSafeInteger(n).
function isSafeInt(n) {
return Number.isSafeInteger(n);
}
console.log(isSafeInt(Number.MAX_SAFE_INTEGER));
console.log(isSafeInt(Number.MAX_SAFE_INTEGER + 1));
Expected output:
true
false
Challenge: Improve nearlyEqual(a, b) so it uses a relative tolerance instead of only Number.EPSILON. Hint: compare the difference to a small fraction of the larger absolute value.
Quick check
Question: Which constant tells you the largest integer JavaScript can represent exactly?
Answer: Number.MAX_SAFE_INTEGER.
Summary
JavaScript number properties describe the limits and precision of the Number type. Number.MAX_SAFE_INTEGER protects exact integer behavior, Number.MAX_VALUE describes finite overflow, and Number.EPSILON helps compare floating-point values safely.