isFinite() - JavaScript | MDN

Try it

function div(x) {
  if (isFinite(1000 / x)) {
    return "Number is NOT Infinity.";
  }
  return "Number is Infinity!";
}

console.log(div(0));
// Expected output: "Number is Infinity!""

console.log(div(1));
// Expected output: "Number is NOT Infinity."

Syntax

Parameters

value

The value to be tested.

Return value

false if the given value is NaN, Infinity, or -Infinity after being converted to a number; otherwise, true.

Description

isFinite() is a function property of the global object.

When the argument to the isFinite() function is not of type Number, the value is first coerced to a number, and the resulting value is then compared against NaN and ±Infinity. This is as confusing as the behavior of isNaN — for example, isFinite("1") is true.

Number.isFinite() is a more reliable way to test whether a value is a finite number value, because it returns false for any non-number input.

Examples

Using isFinite()

js

isFinite(Infinity); // false
isFinite(NaN); // false
isFinite(-Infinity); // false

isFinite(0); // true
isFinite(2e64); // true
isFinite(910); // true

// Would've been false with the more robust Number.isFinite():
isFinite(null); // true
isFinite("0"); // true

Specifications

Specification
ECMAScript® 2027 Language Specification
# sec-isfinite-number

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.