Home > Web Front-end > JS Tutorial > How to use sign function

How to use sign function

不言
Release: 2019-02-15 11:08:40
Original
4496 people have browsed it

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.

How to use sign function

Let’s take a look at the basic syntax of the sign function

Math.sign(x)
Copy after login

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>
Copy after login

The effect is as follows

How to use sign function

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>
Copy after login

The running results are as follows:

NaN
NaN
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template