JavaScript – Math abs 方法
描述
此方法返回一个数的绝对值。
语法
其语法如下−
Math.abs( x ) ;
参数详解
x − 一个数。
返回值
返回一个数的绝对值。
示例
尝试以下示例程序。
<html>  
   <head>
      <title>JavaScript Math abs() Method</title>
   </head>
   <body>   
      <script type = "text/javascript">
         var value = Math.abs(-1);
         document.write("第一个测试值: " + value ); 
         var value = Math.abs(null);
         document.write("<br />第二个测试值: " + value ); 
         var value = Math.abs(20);
         document.write("<br />第三个测试值: " + value ); 
         var value = Math.abs("string");
         document.write("<br />第四个测试值: " + value ); 
      </script>   
   </body>
</html>
输出
第一个测试值: 1
第二个测试值: 0
第三个测试值: 20
第四个测试值: NaN 
极客教程