Number.isFinite()
Tests whether a value is a number that is neither Infinity, -Infinity nor NaN.
Number.isFinite(value)- Added in JavaScript ES2015
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | any | The value to be tested for finiteness. |
Return value
boolean`true` if the value is of type number and is finite, `false` otherwise.
This is the check you want after any arithmetic that could blow up: division, exponentiation, a parse, a sum over user data. It returns true only for a value that is already a number and is not Infinity, -Infinity or NaN.
The difference from global isFinite()
The global isFinite() converts its argument to a number first. That means isFinite("0"), isFinite(null), isFinite("") and isFinite([]) are all true — none of which is a finite number in any useful sense. Number.isFinite() does no conversion, so all of those are false. Prefer it unless you specifically want the coercion, in which case make it explicit with Number.isFinite(Number(x)) so the intent is visible.
When to reach for it
Guarding a computed ratio before it reaches a chart axis, a CSS length or a JSON payload — JSON.stringify silently turns Infinity and NaN into null, so an unguarded value disappears rather than erroring. It is also the right filter for parsed input: Number.parseFloat() returns NaN on failure, which Number.isFinite() catches along with overflow in one test.
Example
When to use it
- Reject a division result before it reaches a chart scale
- Validate parsed user input in one check covering NaN and overflow
- Keep Infinity out of an object about to be JSON serialised
Frequently asked questions
What is the difference between Number.isFinite() and the global isFinite()?
Number.isFinite() does not convert its argument, so only actual numbers can return true. The global isFinite() coerces first, making isFinite('0') and isFinite(null) both true.
Does Number.isFinite() return false for NaN?
Yes. NaN is not finite, so a single Number.isFinite() call rules out NaN, Infinity and -Infinity together.
How do I check a string is a valid finite number?
Convert explicitly first: Number.isFinite(Number(str)). Note that Number('') is 0, so trim and reject empty strings separately if that matters.
More examples
Guarding a computed ratio
One check covers all three failure modes of division, which is why it beats a separate isNaN() test plus two Infinity comparisons.
function ratio(a, b) {
const r = a / b;
return Number.isFinite(r) ? r : 0;
}
console.log(ratio(10, 4)); // 2.5
console.log(ratio(10, 0)); // 0 — Infinity replaced
console.log(ratio(0, 0)); // 0 — NaN replaced
console.log(ratio(-10, 0)); // 0 — -Infinity replacedValidating parsed input
Parsing then checking finiteness rejects both unparseable text and values that overflow the double range, in a single condition.
const inputs = ['3.5', 'abc', '1e400', '', ' 7 '];
for (const s of inputs) {
const n = Number.parseFloat(s);
console.log(JSON.stringify(s), n, Number.isFinite(n));
}
// "3.5" 3.5 true
// "abc" NaN false
// "1e400" Infinity false
// "" NaN false
// " 7 " 7 trueWhy JSON makes this matter
JSON.stringify converts Infinity and NaN to null without warning, so a bad value reaches the consumer disguised as a legitimate absence unless you filter first.
const stats = { avg: 10 / 3, max: 1 / 0, count: 0 / 0 };
console.log(JSON.stringify(stats));
// {"avg":3.3333333333333335,"max":null,"count":null}
const clean = Object.fromEntries(
Object.entries(stats).filter(([, v]) => Number.isFinite(v)),
);
console.log(JSON.stringify(clean)); // {"avg":3.3333333333333335}
Discussion