--- title: Math.sqrt() slug: Web/JavaScript/Reference/Global_Objects/Math/sqrt translation_of: Web/JavaScript/Reference/Global_Objects/Math/sqrt ---
{{JSRef}}

Math.sqrt() 函数返回一个数的平方根,即:

x0,Math.sqrt(x)=x=the uniquey0such thaty2=x\forall x \geq 0, \mathtt{Math.sqrt(x)} = \sqrt{x} = \text{the unique} \; y \geq 0 \; \text{such that} \; y^2 = x

{{EmbedInteractiveExample("pages/js/math-sqrt.html")}}

语法

Math.sqrt(x) 

参数

x
一个数值

描述

如果参数 number 为负值,则 sqrt 返回{{jsxref("NaN")}}。

由于 sqrt 是 Math 的静态方法,所以应该像这样使用:Math.sqrt(),而不是作为你创建的 Math 实例的方法。

示例

使用 Math.sqrt

Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095

Math.sqrt(1);  // 1
Math.sqrt(0);  // 0
Math.sqrt(-1); // NaN
Math.sqrt(-0); // -0

规范

规范版本 规范状态 注解
ECMAScript 1st Edition. Implemented in JavaScript 1.0 Standard Initial definition.
{{SpecName('ES5.1', '#sec-15.8.2.17', 'Math.sqrt')}} {{Spec2('ES5.1')}}
{{SpecName('ES6', '#sec-math.sqrt', 'Math.sqrt')}} {{Spec2('ES6')}}

浏览器兼容性

{{Compat("javascript.builtins.Math.sqrt")}}

参见