JavaScript 中的 Math.sign() 函数
htmljavascriptweb developmentfront end technology更新于 2024/10/12 21:00:17
Math 对象的 sign() 函数接受一个整数并返回其符号。(正数、负数)
- 如果给定的数字是正整数,则此函数返回 +1。
- 并且,如果给定的数字是负整数,则此函数返回 -1。
语法
其语法如下
Math.sign();
示例
<html>
<head>
<title>JavaScript 示例</title>
</head>
<body>
<script type="text/javascript">
var result = Math.sign(-128);
document.write("结果: "+result);
</script>
</body>
</html>
输出
结果: -1