JavaScript | Number.isFinite() function

The Number.isfinite() method in JavaScript is used to check whether the passed value is a finite number or not.
The Number.isfinite() method is different from the isfinite() function since this method doesn’t forcibly convert the parameter to a number and it does not return true for any value that is not of the type number. On the other hand the global isfinite() function converts the tested value to a number first and then tests it.

Syntax:

Number.isFinite(value)

Parameters: This function accepts a single parameter value. It is the number which the user wants to check for finiteness.

Return Value: The number.isfinite() function returns a boolean value i.e. either true or false. It returns true if the passed value is of the type Number, and equates to a finite number else it returns false.

Below are some examples to illustrate the Number.isFinite() function in JavaScript:

  • Passing a negative number as an argument: If the negative number passed to the function is finite then the function will return true otherwise false.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
        document.write(Number.isFinite(-2));          
    </script>

    chevron_right

    
    

    Output:

    true
  • Passing a positive number as an argument: If the positive number passed to the function is finite then the function will return true otherwise false.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
        document.write(Number.isFinite(2));          
    </script>

    chevron_right

    
    

    Output:

    true
  • Passing zero as an argument: If zero is passed to the function then the function will return true as zero is a finite number.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
        document.write(Number.isFinite(0));          
    </script>

    chevron_right

    
    

    Output:

    true
  • Passing an equation as an argument: If the equation evaluates to a finite number then the function will return true otherwise false.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
        document.write(Number.isFinite(7-3+2));          
    </script>

    chevron_right

    
    

    Output:

    true
  • Passing an equation (resulting in inifinte) as an argument: If the equation after evaluating does not gives a finite numeric value then the function will return false.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
        document.write(Number.isFinite(0/0));          
    </script>

    chevron_right

    
    

    Output:

    false
  • Passing a word as an argument: Since a word is not of type integer, so the Number.isFinite() will not convert it to number and will return false.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
        document.write(Number.isFinite("strong"));          
    </script>

    chevron_right

    
    

    Output:

    false
  • Passing number(string) as an argument: Since a string is not of type integer, so the Number.isFinite() will not convert it to number and will return false.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
        document.write(Number.isFinite("5"));          
    </script>

    chevron_right

    
    

    Output:

    false


My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.