File size: 2,184 Bytes
780c9fe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
---
title: Math.sqrt()
short-title: sqrt()
slug: Web/JavaScript/Reference/Global_Objects/Math/sqrt
page-type: javascript-static-method
browser-compat: javascript.builtins.Math.sqrt
sidebar: jsref
---
The **`Math.sqrt()`** static method returns the square root of a number. That is
<!-- prettier-ignore-start -->
<math display="block">
<semantics><mrow><mo>β</mo><mi>x</mi><mo>β₯</mo><mn>0</mn><mo>,</mo><mspace width="0.2777777777777778em"></mspace><mrow><mo lspace="0em" rspace="0.16666666666666666em">πΌπππ.ππππ</mo><mo stretchy="false">(</mo><mi>π‘</mi><mo stretchy="false">)</mo></mrow><mo>=</mo><msqrt><mi>x</mi></msqrt><mo>=</mo><mtext>the unique </mtext><mi>y</mi><mo>β₯</mo><mn>0</mn><mtext> such that </mtext><msup><mi>y</mi><mn>2</mn></msup><mo>=</mo><mi>x</mi></mrow><annotation encoding="TeX">\forall x \geq 0,\;\mathtt{\operatorname{Math.sqrt}(x)} = \sqrt{x} = \text{the unique } y \geq 0 \text{ such that } y^2 = x</annotation></semantics>
</math>
<!-- prettier-ignore-end -->
{{InteractiveExample("JavaScript Demo: Math.sqrt()")}}
```js interactive-example
function calcHypotenuse(a, b) {
return Math.sqrt(a * a + b * b);
}
console.log(calcHypotenuse(3, 4));
// Expected output: 5
console.log(calcHypotenuse(5, 12));
// Expected output: 13
console.log(calcHypotenuse(0, 0));
// Expected output: 0
```
## Syntax
```js-nolint
Math.sqrt(x)
```
### Parameters
- `x`
- : A number greater than or equal to 0.
### Return value
The square root of `x`, a nonnegative number. If `x < 0`, returns {{jsxref("NaN")}}.
## Description
Because `sqrt()` is a static method of `Math`, you always use it as `Math.sqrt()`, rather than as a method of a `Math` object you created (`Math` is not a constructor).
## Examples
### Using Math.sqrt()
```js
Math.sqrt(-1); // NaN
Math.sqrt(-0); // -0
Math.sqrt(0); // 0
Math.sqrt(1); // 1
Math.sqrt(2); // 1.414213562373095
Math.sqrt(9); // 3
Math.sqrt(Infinity); // Infinity
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{jsxref("Math.cbrt()")}}
- {{jsxref("Math.exp()")}}
- {{jsxref("Math.log()")}}
- {{jsxref("Math.pow()")}}
|