Javascript | Math.sign( ) Function

The Math.sign() is a builtin function in JavaScript and is used to know the sign of a number, indicating whether the number specified is negative or positive.

Syntax:

Math.sign(number)

Parameters: This function accepts a single parameter number which represents the number whose sign you want to know.
./
Return Value: The Math.sign() function returns five different values as described below:

  1. It returns 1 if the argument passed is a positive number.
  2. It returns -1 if the argument passed is a negative number.
  3. It returns 0 if the argument passed is a positive zero.
  4. It returns -0 if the argument passed is a negative zero.
  5. If none of the above cases match,it returns Nan.

Examples:

Input : Math.sign(2)
Output : 1
     
Input : Math.sign(-2)
Output : -1

Input : Math.sign(0)
Output : 0

Input : Math.sign(-0)
Output : -0

Input : Math.sign(haa)
Output : NaN

Below programs illustrates the Math.sign() function in JavaScript:



  1. When a positive number is passed as an argument:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    1
  2. When a negative number is passed as an argument:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    -1
  3. When a positive zero is passed as an argument:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    0
  4. When a negative zero is passed as an argument:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

       
    <script type="text/javascript">
        document.write(Math.sign(-0));          
     </script>

    chevron_right

    
    

    Output:

    -0
  5. When an invalid number is passed as an argument:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
       document.write(Math.sign(haa));          
    </script>

    chevron_right

    
    

    Output:

    NaN


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.