JavaScript Reference ยท Number and BigInt
Number.isFinite()
Reference for Number.isFinite(): checking that a value is a real, finite number without type coercion, and how it differs from the global isFinite().
Number.isFinite() returns true only when the argument is of type number and is neither NaN, Infinity nor -Infinity. It never converts its argument, so any non-number gives false.
The older global isFinite() first converts its argument with Number(), which makes isFinite("12") and isFinite(null) return true. The method on Number is the stricter and safer choice.
Number.isFinite(value)
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
Number.isFinite(value)
| Name | Description |
|---|---|
value |
The value to test. Any type is accepted. |
Return value: true if value is a number that is not NaN and not infinite; otherwise false.
Exceptions: none. (The global isFinite() throws a TypeError for BigInt and Symbol values because it converts them first.)
Usage notes
- No coercion:
Number.isFinite("5")isfalse. Convert first if you want to accept numeric strings, for exampleNumber.isFinite(Number(input)). - Global version quirks:
isFinite(""),isFinite(null)andisFinite(true)are alltruebecause they convert to0or1. - What fails the test:
NaN,Infinityand-Infinity, plus every non-number type. - Typical uses: validating the result of a division or
Mathfunction, rejecting bad values before drawing a chart or setting a CSS number, and filtering datasets. - Division by zero gives
InfinityorNaN, not an exception. - Overflow: results beyond
Number.MAX_VALUEbecomeInfinity, so a finite check catches them. - Integers: a finite number need not be an integer; for that use
Number.isInteger(). - JSON:
JSON.stringify()turnsNaNandInfinityintonull, so check before serializing.
Browser support
Supported in all current browsers; it arrived with ES2015.