Math对象为数学常量和函数提供属性和方法。与其他全局对象不同,Math不是构造函数。Math的所有属性和方法都是静态的,可以通过将Math作为对象来调用,而无需创建它。本文主要介绍JavaScript(JS) Math.sqrt( x ) 方法。

1、描述

这个方法返回一个数字的平方根。 如果一个数字的值是负数,则sqrt返回NaN

2、语法

其语法如下,

Math.sqrt( x ) ;

3、参数

:一个数字

4、返回值

返回给定数字的平方根。

5、使用示例

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

6、输出

Math.sqrt( 0.5 ) : 0.7071067811865476
Math.sqrt( 81 ) : 9
Math.sqrt( 13 ) : 3.605551275463989
Math.sqrt( -4 ) : NaN

推荐文档