Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
Like Article
  • Last Updated : 02 Jan, 2023
Improve Article
Save Article
Like Article

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.

1. 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.

Syntax:

isNan(parameter)

 Example 1: This method shows the usage of the Javascript isNan() method.

Javascript




<script>
    function example(x) {
        if(isNaN(x)) {
            return 'It is NaN';
        }else {
            return 'It isnt NaN';
        }
    }
     
    // It is not NaN
    console.log(example(13));
     
    // It is NaN
    console.log(example('GeeksForGeeks'));
</script>


Output:

It isnt NaN
It is NaN

Note: The isNaN() function can alternatively be used as Number.isNaN(). It is considered to be a more robust version of the original.

Example 2: In this example, we will check for some arguments if they are NaN or not.

Javascript




<script>
    function example(x){
        if(Number.isNaN(x)){
            return 'It is NaN';
        }else{
            return 'It isnt NaN';
        }
    }
     
    // It is not NaN
    console.log(example(999));
     
    // It is NaN
    console.log(example('2C'));
</script>


Output:

It isnt NaN
It isnt NaN

Note: isNaN() (or Number.isNaN()) can be used to deduce if a number is NaN or not but it doesn’t says anything about the number being finite.

2. isFinite() Method: 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




<script>
    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
</script>


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




<script>
    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
</script>


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




<script>
    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
</script>


Output:

Number is finite
Number is finite
Number is not finite

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!