isFinite()
Tests whether a value is a finite number after being coerced to a number.
isFinite(value)- Added in JavaScript ECMAScript 1 (1997)
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | any | The value to be tested. It is converted to a number before the test. |
Return value
booleanfalse if the value converts to NaN, Infinity or -Infinity; true otherwise.
Errors
Throws TypeError if the value is a BigInt or a Symbol, since neither can be converted to a number.
Changelog
- PHP ECMAScript 2015Number.isFinite() was added as the non-coercing alternative and is the preferred check in new code.
isFinite() coerces first, then asks whether the resulting number is neither NaN nor an infinity. The coercion is the part that bites: isFinite(null), isFinite(""), isFinite([]) and isFinite(true) are all true, because each converts to 0 or 1.
Prefer Number.isFinite
Number.isFinite() returns false for anything that is not already a number, so it doubles as a type check. Use the global version only when you genuinely want string input to be parsed, and use Number.isFinite(Number(x)) if you want that behaviour spelled out.
Where it earns its place
Arithmetic in JavaScript never throws on overflow or division by zero — it produces Infinity and keeps going. A finiteness check is the guard that stops an Infinity from spreading through a running total, a chart axis, or a value on its way to JSON.stringify(), which quietly serialises both Infinity and NaN as null.
Note that it is finiteness, not safety: Number.MAX_VALUE and 1e308 are finite but far past the range where integers stay exact. For that question use Number.isSafeInteger().
Example
When to use it
- Guard a computed average or ratio before displaying it, so a divide-by-zero never renders as Infinity.
- Validate a numeric config value that must be a real bounded number.
- Filter out non-finite entries before summing an array of measurements.
Frequently asked questions
What is the difference between isFinite and Number.isFinite?
Number.isFinite returns false for any value that is not already of type number, while the global isFinite converts first. Number.isFinite("42") is false; isFinite("42") is true.
Why does isFinite(null) return true?
Because Number(null) is 0, which is finite. Use Number.isFinite if you want null, empty strings and booleans to be rejected.
How do I detect division by zero in JavaScript?
Check the result with Number.isFinite, because the division itself never throws — n / 0 evaluates to Infinity and 0 / 0 evaluates to NaN, both of which Number.isFinite reports as false.
More examples
Guarding a ratio
Division by zero produces Infinity rather than throwing, and JSON.stringify turns it into null, so the bad value reaches the wire silently unless you check for it.
function rate(hits, total) {
const r = hits / total;
return Number.isFinite(r) ? r : 0;
}
console.log(rate(3, 4)); // 0.75
console.log(rate(3, 0)); // 0 — would have been Infinity
console.log(rate(0, 0)); // 0 — would have been NaN
console.log(3 / 0); // Infinity — no error is thrown
console.log(JSON.stringify({ r: 3 / 0 })); // {"r":null}Coercion side by side
The global isFinite is true for every value that coerces to a finite number, including "", null, true and []. Number.isFinite is true only for the number 0, since every other entry is not of type number.
const values = [0, "0", "", " ", null, undefined, true, [], [7], {}, "1e400"];
for (const v of values) {
console.log(String(JSON.stringify(v)), isFinite(v), Number.isFinite(v));
}Finite is not the same as safe
A value can be finite and still be past the point where consecutive integers are distinguishable. Number.isSafeInteger is the check for integer exactness; isFinite only rules out NaN and the infinities.
const big = Number.MAX_VALUE;
console.log(Number.isFinite(big)); // true
console.log(Number.isFinite(big * 2)); // false — overflowed to Infinity
console.log(Number.isFinite(2 ** 53 + 1)); // true
console.log(Number.isSafeInteger(2 ** 53 + 1)); // false
console.log(2 ** 53 === 2 ** 53 + 1); // true — precision already gone
Discussion