无涯教程-Javascript - sqrt()函数

99 阅读1分钟

此方法返回数字的平方根,如果数字的值为负,则sqrt返回NaN。

sqrt() - 语法

Math.sqrt( x ) ;

x - 数值

sqrt() - 返回值

返回给定数字的平方根。

sqrt() - 示例

<html>   
   <head>
      <title>JavaScript Math sqrt() Method</title>
   </head>
   
   <body>      
      <script type="text/javascript">
         var value=Math.sqrt( 0.5 );
         document.write("First Test Value : " + value );
         
         var value=Math.sqrt( 81 );
         document.write("<br />Second Test Value : " + value ); 
         
         var value=Math.sqrt( 13 );
         document.write("<br />Third Test Value : " + value ); 
         
         var value=Math.sqrt( -4 );
         document.write("<br />Fourth Test Value : " + value ); 
      </script>      
   </body>
</html>

运行上面代码输出

First Test Value : 0.7071067811865476
Second Test Value : 9
Third Test Value : 3.605551275463989
Fourth Test Value : NaN  

参考链接

www.learnfk.com/javascript/…