Math.sqrt
Returns the square root of a number.
Available since: ES1
Syntax
Math.sqrt(x)Parameters
| Parameter | Type | Description |
|---|---|---|
| x | number | A non-negative number. |
Return value
numberThe square root, or NaN for a negative number.
Examples
A square root:
console.log(Math.sqrt(16));
console.log(Math.sqrt(2));Output:
4
1.4142135623730951The distance between two points:
const dx = 6 - 3;
const dy = 8 - 4;
console.log(Math.sqrt(dx ** 2 + dy ** 2));Output:
5Notes
There is Math.cbrt() for the cube root; for any other root use x ** (1 / n).
See also
- Math.pow — Raises a number to a power.
- Exponentiation (**) — Raises a number to a power.
In the tutorial
- Numbers — Numbers in JavaScript — integers and fractions, rounding, the Math object, toFixed and the special values Infinity and NaN.