Sub topic 8
Validating Numbers with Number.isNaN, isFinite, isInteger, and isSafeInteger
This chapter assumes you already know how Number() converts strings, what NaN and Infinity are, and where the safe integer range ends. The focus here is on checking a value once you have it. That means answering questions like "is this a usable number?" and "is this a whole number I can count with?"
The four static checks
Each method lives on the Number constructor. None of them convert their argument. If the value is not already of type number, the result is false.
| Method | Returns `true` when the value is... | `"42"` | `NaN` | `Infinity` | `4.5` | `2 ** 53` |
|---|---|---|---|---|---|---|
| `Number.isNaN` | exactly `NaN` | false | true | false | false | false |
| `Number.isFinite` | a number that is neither `NaN` nor ±`Infinity` | false | false | false | true | true |
| `Number.isInteger` | a finite number with no fractional part | false | false | false | false | true |
| `Number.isSafeInteger` | an integer from `-(2**53 - 1)` to `2**53 - 1` | false | false | false | false | false |
The checks get stricter as you go down the table. Each check that passes also implies that the checks above it gave the "good" answer.
The global versions coerce
The older global functions isNaN and isFinite convert their argument to a number first. Because of that, they answer a different question.
console.log(isFinite("42")); // true ("42" becomes 42)
console.log(Number.isFinite("42")); // false (a string is not a number)
console.log(isFinite(null)); // true (null becomes 0)
console.log(Number.isFinite(null)); // false
console.log(Number.isInteger(5.0)); // true (5.0 and 5 are the same value)
console.log(Number.isInteger(5.5)); // false
console.log(Number.isInteger(1e300)); // true, but far outside the safe range
console.log(Number.isSafeInteger(1e300)); // false
console.log(Number.isSafeInteger(2 ** 53 - 1)); // trueA validation pattern: convert, then check
Conversion and validation are separate steps. First convert with Number(), then run the strictest check your use case needs. Add a guard for empty strings, because Number("") returns 0.
function parseQuantity(input) {
if (typeof input !== "string" || input.trim() === "") return null;
const n = Number(input);
if (!Number.isSafeInteger(n) || n < 1) return null;
return n;
}
console.log(parseQuantity(" 7 ")); // 7
console.log(parseQuantity("1e3")); // 1000
console.log(parseQuantity("3.5")); // null
console.log(parseQuantity("abc")); // null
console.log(parseQuantity("")); // null
console.log(parseQuantity("-2")); // null
console.log(parseQuantity("9007199254740993")); // null (rounds to 2 ** 53)Common errors
- Using
x === NaN. This comparison is alwaysfalse. UseNumber.isNaN(x). - Using
isFiniteon raw input. The global version accepts"42","", andnull. Convert explicitly, then callNumber.isFinite. - Treating
isIntegeras "safe to count with". Large integers passisIntegereven though nearby values can no longer be told apart. - Forgetting that
typeof NaNis"number". Atypeofcheck alone does not prove that a value is usable.
Exercise
What do Number.isFinite("10"), isFinite("10"), Number.isInteger(10.0), and Number.isSafeInteger(2 ** 53) return, and why?
Show answer
false, true, true, false. Number.isFinite does not coerce, so the string fails. The global isFinite converts "10" to 10 first. 10.0 is stored as the integer 10. 2 ** 53 is one past Number.MAXSAFEINTEGER, so it is not a safe integer.
Exercise
Write a function isValidPrice(x) that accepts only finite numbers of type number that are zero or greater.
Show answer
function isValidPrice(x) { return Number.isFinite(x) && x >= 0; } Number.isFinite rejects non-numbers, NaN, and both infinities without coercion, and the comparison then rules out negative values.