JavaScript (ES2026) reference · Math Methods and Constants
JavaScript Math.sqrt() Method
Return the square root of a number, or NaN for negative numbers. Use it for distances and the Pythagorean theorem.
JavaScript (ES2026) Since ES1
Syntax
Math.sqrt(x)
Parameters
| Parameter | Description |
|---|---|
x |
A number. |
Return value
A number or NaN.
Examples
console.log(Math.sqrt(16), Math.sqrt(2), Math.sqrt(-1));
Output
4 1.4142135623730951 NaN
const dist = (a, b) => Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2);
console.log(dist({ x: 0, y: 0 }, { x: 3, y: 4 }));
Output
5
Version history
| Version | Change |
|---|---|
| JavaScript ES1 | Added |
Related
Math.cbrt()· Return the cube root of a number; unlike Math.pow(x, 1/3) it works for negative numbers.Math.hypot()· Return the square root of the sum of squares of its arguments, e.g. the length of a vector or a diagonal.Math.pow()· Return a base raised to an exponent. The ** operator does the same: Math.pow(2, 10) equals 2 ** 10.- JavaScript Math · tutorial
- JavaScript Numbers · tutorial