How to use isFinite() Method In Javascript

To determine whether a number is finite we can use the isFinite() function. It is a boolean function that returns true if a number is Finite otherwise false.

Syntax:

isFinite(parameter);

Example 1: This method shows the use of the isFinite() method in Javascript.

Javascript




function example(x) {
    if (isFinite(x)) {
        return 'Number is finite';
    }
    return 'Number is not finite';
}
 
console.log(example('2021/10/29'));
// Number is not finite
 
console.log(example(29));
// Number is finite


Output:

Number is not finite
Number is finite

Example 2: In this example, we will check for numbers if they are finite or not.

Javascript




function example(x) {
    if (isFinite(5 / x)) {
        return 'Number is finite';
    }
    return 'Number is not finite';
}
 
console.log(example(0));
// Number is not finite
 
console.log(example(10));
    // Number is finite


Output:

Note: If needed, the isFinite() function can parse the parameter into the number

Example 3: In this example, we will check for numbers if they are finite or not using the isFinite() method of Javascript.

Javascript




function example(x) {
    if (isFinite(x)) {
        return 'Number is finite';
    }
    return 'Number is not finite';
}
 
console.log(example('123'));
// Number is finite
 
console.log(example(133));
// Number is finite
 
console.log(example('123D'));
// Number is not finite


Output:

Number is finite
Number is finite
Number is not finite

How to check whether a number is NaN or finite in JavaScript ?

In this article, we will see how to check whether the number is NaN or finite. To check whether the given number is NaN or finite, we can use JavaScript methods.

These are the following methods for solving this problem:

Table of Content

  • Using isNaN() Method
  • Using isFinite() Method
  • Using Lodash isFinite() and _.isNaN() Methods

Similar Reads

Using isNaN() Method

To determine whether a number is NaN, we can use the isNaN() function. It is a boolean function that returns true if a number is NaN otherwise returns false....

Using isFinite() Method

...

Using Lodash isFinite() and _.isNaN() Methods

To determine whether a number is finite we can use the isFinite() function. It is a boolean function that returns true if a number is Finite otherwise false....