Number.isFinite()

The single best "is this a usable number" check. It excludes the three values that break arithmetic — NaN and both infinities — and unlike the global version it does not convert its argument first.

Number static methodES2015Live demo
Common call
Number.isFinite(x)
Returns
boolean
Replaces
typeof x === "number" && !isNaN(x) && x !== Infinity
Watch out
the global isFinite coerces; this does not
Number.isFinite(valuevalue — Any value. Non-numbers are false without conversion, as are NaN, Infinity and -Infinity.type: any · required)
→ boolean

Demo

Live evaluation
Try:
Inputs
vanyany value
Output
[Number.isFinite(42), isFinite(42)]
[true, true]

The pair is [Number.isFinite(v), isFinite(v)]. For genuine numbers the two agree, and for non-numeric text both are false. The divergence appears with a numeric STRING: the global converts '5' to 5 and answers true, while the static answers false because a string is not a number. The auto input here turns numeric-looking text into a real number first, so see the examples below for the raw-string contrast. Neither form can be shown Infinity through a text box.

Parameters

NameTypeRequiredDescription
valueanyyesAny value. Non-numbers are false without conversion, as are NaN, Infinity and -Infinity.

Return value

boolean — True only for a number that is neither infinite nor NaN. No coercion — a numeric string is false.

Common patterns

Validate a converted value
Catches NaN and Infinity in one test.
const n = Number(input);
if (!Number.isFinite(n)) reject();
Detect overflow after a computation
Infinity is how doubles report overflow.
if (!Number.isFinite(result)) throw new Error("overflow");
Guard a division
Division by zero gives Infinity, not an error.
const avg = count ? total / count : 0;

Examples

1. A number
Number.isFinite(42)
Returns
true
2. A numeric string
Number.isFinite('5')
Returns
false
3. The global coerces
isFinite('5')
Returns
true
4. Infinity
Number.isFinite(Infinity)
Returns
false
5. Division by zero
Number.isFinite(1 / 0)
Returns
false
6. NaN too
Number.isFinite(NaN)
Returns
false

Pitfalls

1. The global isFinite coerces first
isFinite("5") is true, isFinite("") is true — the empty string converts to 0 — and isFinite(null) is true for the same reason. Used as a numeric-validity check it accepts values that are not numbers at all.
Accepts non-numbers
isFinite('')
true
Strict
Number.isFinite('')
false
2. Division by zero gives Infinity rather than an error
No exception is thrown, so a zero denominator produces Infinity that flows onward through the calculation. It then contaminates averages, percentages and chart scales in ways that surface far from the division.
Silent
10 / 0
Infinity
Guard it
denominator === 0 ? 0 : 10 / denominator
0
3. It is not a "this is numeric" test for strings
Validating form input with it rejects everything, because input arrives as text. Convert with Number first — and remember that an empty string converts to 0 and therefore passes.
Rejects valid text
Number.isFinite('42')
false
Convert, and guard blank
const s = '42';
s.trim() !== '' && Number.isFinite(Number(s))
true
4. Infinity survives many operations
Infinity minus a number is still Infinity, and only Infinity minus Infinity becomes NaN. A single overflow therefore propagates quietly for a long time before it turns into the more obvious NaN.
Still infinite
Number.isFinite(Infinity - 1000)
false
Check early
if (!Number.isFinite(x)) handle();
caught at the source

When to use

Use it
  • Validating that a converted value is usable in arithmetic
  • Detecting overflow or division by zero
  • Guarding values before they reach a chart, a layout calculation or a database
  • Any check where NaN and Infinity should both fail
Reach for something else
  • The value is a string → convert with Number first
  • You only care about NaN → Number.isNaN
  • You need a whole number → Number.isInteger
  • The global isFinite → it coerces, and rarely helps

Notes

Complexity
O(1)
Return
A boolean; nothing is allocated or coerced
CPython impl
V8: Builtins-number-isfinite
Memory
No allocation
Thread-safe
Single-threaded

FAQ

Because it converts its argument first, so it answers true for numeric strings, for the empty string and for null. That makes it a loose "could this be a number" test rather than a check on the value you actually hold.

isFinite('');          // true  — '' converts to 0
Number.isFinite('');   // false

History

ES1
The global isFinite present from the first version, with coercion.
ES2015
Number.isFinite added as the non-coercing version.