Skip to content
Related Articles
Open in App
Not now

Related Articles

Javascript Math.sign() Function

Improve Article
Save Article
  • Last Updated : 30 Dec, 2022
Improve Article
Save Article

The Math.sign() is a built-in 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:

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

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

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

Javascript




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


Output:

1

Example 2: When a negative number is passed as an argument: 

Javascript




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


Output:

-1

Example 3: When a positive zero is passed as an argument: 

Javascript




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


Output:

0

Example 4: When a negative zero is passed as an argument: 

Javascript




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


Output:

-0

Example 5: When an invalid number is passed as an argument: 

Javascript




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


Output:

NaN

We have a complete list of Javascript Math Objects methods, to check those please go through this Javascript Math Object Complete reference article.

Supported Browsers:

  • Google Chrome 1 and above
  • Internet Explorer 3 and above
  • Firefox 1 and above
  • Opera 3 and above
  • Safari 1 and above

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!