JavaScript | Number.isInteger( ) function

The Number.isInteger() function in JavaScript is used to check whether the value passed to it is an integer or not. It returns true if the passed value is an integer, otherwise it return false.

Syntax:

Number.isInteger(value)

Parameters: This function accepts a single parameter value which specifies the number which the user wants to check for integer.

Return Value: The number.isInteger() function returns a boolean value ,i.e. either true or false. It will return true if the passed value is of the type Number and an integer, else it returns false.

Below examples illustrates the Number.isInteger() function in JavaScript:



  1. Passing a negative number as an argument: If a negative integer value is passed to the function as argument then the function will return true, if the negative value passed to it is not of integer type then the function will return false.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    true
    false
    
  2. Passing a positive number as an argument: If a positive integer value is passed to the function as argument then the function will return true, if the positive value passed to it is not of integer type then the function will return false.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    true
  3. Passing a zero as an argument: If zero is passed to the Number.isInteger() function then it will return true as zero is also an integer.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    true
  4. Passing a number consisting of decimal places as an argument: If a decimal number is passed as argument, the function will return false.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    false
  5. Passing a string as an argument: If the argument passed to the Number.isInteger() function is of type string then it will return false.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
        document.write("Output : " + Number.isInteger("hi"));          
    </script>

    chevron_right

    
    

    Output:

    false


My Personal Notes arrow_drop_up

Recommended Posts:



    Article Tags :
    Practice Tags :


    Be the First to upvote.


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