totn JavaScript

JavaScript: Number isFinite() method

This JavaScript tutorial explains how to use the Number method called isFinite() with syntax and examples.

Description

In JavaScript, isFinite() is a Number method that is used to return a Boolean value indicating whether a value is finite number. Because isFinite() is a method of the Number object, it must be invoked through the object called Number.

Syntax

In JavaScript, the syntax for the isFinite() method is:

Number.isFinite(value);

Parameters or Arguments

value
The value to test whether it is a finite number.

Returns

The isFinite() method returns true if the value is an infinite number. Otherwise, it returns false.

If the isFinite() method is passed a non-numeric value, it will return false. This is because the isFinite() method does NOT convert the value to a number before testing whether it is a finite number.

Note

  • The isFinite() method does NOT convert the value parameter to a numeric value before determining whether the value is finite.

Example

Let's take a look at an example of how to use the isFinite() method in JavaScript.

For example:

console.log(Number.isFinite(3.5));
console.log(Number.isFinite('3.5'));

In this example, we have invoked the isFinite() method using the Number class.

We have written the output of the isFinite() method to the web browser console log, for demonstration purposes, to show what the isFinite() method returns.

The following will be output to the web browser console log:

true
false

In this example, the first output to the console log returned true since 3.5 is a finite number.

However, the second output to the console log returned false since the string value '3.5' is not a number and the isFinite() method does not convert the value to a number before testing whether it is a finite number.