When you’re writing numeric code, you often need a safe guard: “Is this thing really a finite number?” Number.isFinite(value) gives you just that. It returns true only when the value is of the JavaScript Number type and is neither Infinity, -Infinity, nor NaN. It will not coerce non-number inputs. It’s more precise and less error-prone than the old global isFinite().
Number.isFinite(123); // true
Number.isFinite(0); // true
Number.isFinite(-1e308); // true
Number.isFinite(Infinity); // false
Number.isFinite(-Infinity); // false
Number.isFinite(NaN); // false
Number.isFinite("123"); // false (no coercion)
Number.isFinite(null); // false
Number.isFinite(undefined); // falseWhat the global isFinite does differently
The legacy global function isFinite(value) first converts its argument to a Number via Number(value). That means strings like "123" or null (converted to 0) pass the test, which often surprises people. By contrast, Number.isFinite avoids coercion entirely and demands that a value is already a Number. Use Number.isFinite when you care about the actual type, not coercibility.
isFinite("123"); // true (coerced to 123)
Number.isFinite("123"); // false
isFinite(null); // true (null → 0)
Number.isFinite(null); // falseCore behavior as defined by the spec
Under the ECMAScript specification, Number.isFinite behaves roughly like:
Return true if
typeof value is "number"
and value is not NaN
and value is neither +Infinity nor -Infinity
otherwise return false.Non-number types immediately yield false. That’s what makes it safe to use after parsing or arithmetic operations—no extra conversion surprises.
Why you need it – practical usage
When you’re consuming external data (user input, API responses, etc.), you might coerce or parse something into a number. After that, you often need to be sure it resulted in a “real” finite number before using it in calculations:
function safeDivide(a, b) {
const x = Number(a);
const y = Number(b);
if (!Number.isFinite(x) || !Number.isFinite(y)) {
throw new Error("Bad inputs");
}
const result = x / y;
if (!Number.isFinite(result)) {
throw new Error("Division result not finite");
}
return result;
}
safeDivide("100", "5"); // 20
safeDivide("100", "0"); // throws (division by zero → Infinity)
safeDivide("foo", "5"); // throws (invalid input)You can also use it to filter arrays or validate values:
const mixed = [0, 1, Infinity, "2", NaN, 3.14];
const onlyFiniteNumbers = mixed.filter(Number.isFinite);
// → [0, 1, 3.14]Edge cases and nuances
Number.isFinite accepts all real (IEEE-754) numbers—including fractional values. That contrasts with Number.isInteger, which returns false for non-integer finite numbers:
Number.isFinite(1.1); // true
Number.isInteger(1.1); // falseBecause Number.isFinite doesn’t coerce, passing wrapper objects or boxed numbers also returns false:
Number.isFinite(new Number(42)); // falseIt’s worth noting browser compatibility: Number.isFinite is widely supported in modern environments, but not in older ones (especially older Internet Explorer versions). A simple polyfill is:
if (Number.isFinite === undefined) {
Number.isFinite = function(value) {
return typeof value === "number" && isFinite(value);
};
}Here the fallback uses the global isFinite internally only after confirming value is a number.
Mental model and usage advice
Use Number.isFinite when you already have or will produce a Number and want to ensure it’s a valid, usable number. Avoid the global isFinite unless you intend the coercion behavior (which is rarely safe in robust code). Combine Number.isFinite with Number.isNaN, or just test that value === value && value !== Infinity && value !== -Infinity, but the built-in is clearer and optimized.
function isGoodNumber(x) {
return Number.isFinite(x);
}
isGoodNumber(42); // true
isGoodNumber(NaN); // false
isGoodNumber("42"); // false
isGoodNumber(Infinity); // falseWhen writing libraries or APIs where you accept numeric inputs that may be malformed, a validation step using Number.isFinite gives you a strong gate before doing anything dangerous with x + y, 1/x, Math.log, etc.
References
| Source | What it covers |
|---|---|
| MDN: Number.isFinite() | Definition, behavior, examples, difference from global isFinite. |
| MDN: isFinite() | How it coerces input first and why that behavior is less safe. |
| Can I use: Number.isFinite | Browser support information for Number.isFinite. |
| StackOverflow: difference between Number.isFinite and Number.isInteger | Clarifies that Number.isFinite accepts fractional values while isInteger does not. |