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:
- 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.
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:
- Example 1: When a positive number is passed as an argument.
<scripttype="text/javascript">document.write(Math.sign(2));</script>chevron_rightfilter_noneOutput:
1
- Example 2: When a negative number is passed as an argument:
<scripttype="text/javascript">document.write(Math.sign(-2));</script>chevron_rightfilter_noneOutput:
-1
- Example 3: When a positive zero is passed as an argument:
<scripttype="text/javascript">document.write(Math.sign(0));</script>chevron_rightfilter_noneOutput:
0
- Example 4: When a negative zero is passed as an argument:
<scripttype="text/javascript">document.write(Math.sign(-0));</script>chevron_rightfilter_noneOutput:
-0
- Example 5: When an invalid number is passed as an argument:
<scripttype="text/javascript">document.write(Math.sign(haa));</script>chevron_rightfilter_noneOutput:
NaN
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
- How to call a function that return another function in JavaScript ?
- JavaScript | toPrecision( ) Function
- How to override a JavaScript function ?
- JavaScript | Array every() function
- JavaScript | toFixed( ) Function
- JavaScript | Function binding
- JavaScript | toString( ) function
- JavaScript | Array.of() function
- JavaScript | String() Function
- Javascript | Number() Function
- What is the inline function in JavaScript ?
- JavaScript | Array some() function
- JavaScript | Math.abs( ) function
- JavaScript | Function Invocation
- JavaScript | toExponential() Function
Supported Browsers: The browsers supported by Javascript Math.sign( ) Function are listed below:
Recommended Posts:
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.

