Number.isFinite
Checks that a value is a finite number (not NaN and not Infinity).
Available since: ES2015
Syntax
Number.isFinite(value)Parameters
| Parameter | Type | Description |
|---|---|---|
| value | any | The value to check. |
Return value
booleantrue if the value is an ordinary finite number.
Examples
Ordinary and special numbers:
console.log(Number.isFinite(42));
console.log(Number.isFinite(1 / 0));
console.log(Number.isFinite(NaN));Output:
true
false
falseValidating user input:
const input = '12.5';
const value = Number(input);
console.log(Number.isFinite(value) ? value * 2 : 'not a number');Output:
25Notes
Strings are not converted: Number.isFinite('42') is false. Convert the string with Number() first.
See also
- isFinite — Checks that a value converts to a finite number.
- Number.isNaN — Checks that a value is NaN, without type conversion.
- Number.isInteger — Checks whether a value is an integer.
Related recipes
In the tutorial
- Numbers — Numbers in JavaScript — integers and fractions, rounding, the Math object, toFixed and the special values Infinity and NaN.