sign is a static method of math. We usually use the form of Math.sign(). Math.sign() is a built-in function in How to use sign function. It is used to return the sign of a number, indicating whether the specified number is negative or positive. number. Let's take a closer look at how to use the sign function.
Let’s take a look at the basic syntax of the sign function
Math.sign(x)
x represents any number.
The Math.sign() function returns five different values, as described below:
Returns 1 if the argument passed is a positive number.
If the parameter passed is a negative number, -1 is returned.
If the passed argument is positive zero, 0 is returned.
If the passed parameter is negative zero, then -0 is returned.
If none of the above conditions match, return Nan.
Let’s look at the specific example of the sign function
The parameters are positive numbers, negative numbers, positive zero, and negative zero
The code is as follows
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script type="text/javascript"> // 正数作为参数传递时: document.write(Math.sign(2)+"<br/>"); // 负数作为参数传递时: document.write(Math.sign(-2)+"<br/>"); // 正零作为参数传递时: document.write(Math.sign(0)+"<br/>"); // 负零作为参数传递时: document.write(Math.sign(-0)); // 无效数字作为参数传递时: </script> </body> </html>
The effect is as follows
When the parameters are invalid
The code is as follows
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script type="text/javascript"> document.write(Math.sign("haa")+"<br/>"); document.write(Math.sign()); </script> </script> </body> </html>
The running results are as follows:
NaN NaN
This article ends here. For more exciting content, you can pay attention to other related column tutorials on the php Chinese website! ! !
The above is the detailed content of How to use sign function. For more information, please follow other related articles on the PHP Chinese website!