JavaScript Reference ยท Global Functions and Values
The isFinite() function
Reference for the global isFinite() function: how it converts its argument to a number, what counts as finite, and when Number.isFinite() is the better test.
isFinite() converts its argument to a number and returns true if the result is an ordinary number: not NaN, not Infinity and not -Infinity. It is a quick guard after a division or a parse.
Because it converts first, strings that look like numbers pass the test. The stricter Number.isFinite() accepts only values that already are numbers.
isFinite(10 / 4); // true isFinite(10 / 0); // false
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
isFinite(value)
| Name | Description |
|---|---|
value |
Any value. It is converted with the same rules as Number(value). |
Return value: false if the converted number is NaN, Infinity or -Infinity; otherwise true.
Exceptions: a TypeError for a Symbol or a BigInt, which cannot be converted to a number implicitly.
Usage notes
- Division by zero gives
Infinity, not an error, for ordinary numbers.0 / 0givesNaN. Both failisFinite(). - Conversion makes it lenient.
isFinite("25")andisFinite(null)aretrue(the second becausenullbecomes0). UseNumber.isFinite()when the value must already be a number. - Blank strings become
0, soisFinite("")istrue. Check for an empty string separately when validating input. - Overflow: results beyond about
1.8e308becomeInfinity, which fails the test:isFinite(Number.MAX_VALUE * 2)isfalse. - Not a type check. A finite number may still be fractional; use
Number.isInteger()when you need a whole number. - BigInt values are never
Infinity, butisFinite(10n)throws; useNumber.isFinite(10n), which simply returnsfalse, or convert deliberately.
Browser support
isFinite() works in all browsers and runtimes, as does Number.isFinite() in current ones.
Related
isNaN(),Infinity,NaN,Number.isFinite(),Number.isInteger()- Lessons: Numbers, Type conversion