Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Number.isFinite

Checks that a value is a finite number (not NaN and not Infinity).

Available since: ES2015

Syntax

Number.isFinite(value)

Parameters

ParameterTypeDescription
valueanyThe 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
false

Validating user input:

const input = '12.5';
const value = Number(input);
 
console.log(Number.isFinite(value) ? value * 2 : 'not a number');

Output:

25

Notes

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.

Thanks for reading!

Donate to the site — we work hard for you.