Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Math.sqrt

Returns the square root of a number.

Available since: ES1

Syntax

Math.sqrt(x)

Parameters

ParameterTypeDescription
xnumberA 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.4142135623730951

The distance between two points:

const dx = 6 - 3;
const dy = 8 - 4;
 
console.log(Math.sqrt(dx ** 2 + dy ** 2));

Output:

5

Notes

There is Math.cbrt() for the cube root; for any other root use x ** (1 / n).

See also

In the tutorial

  • Numbers — Numbers in JavaScript — integers and fractions, rounding, the Math object, toFixed and the special values Infinity and NaN.

Thanks for reading!

Donate to the site — we work hard for you.