JavaScript Tutorial JavaScript References

JavaScript - Math.sqrt() Method



The JavaScript Math.sqrt() method returns the square root of the given number.

Syntax

Math.sqrt(x)

Parameters

x Specify a positive number.

Return Value

Returns the square root of the specified number.
If the x is negative, it returns NaN.

Example:

In the example below, Math.sqrt() method is used to find out the square root of the given number.

var txt;

txt = "Math.sqrt(25) = " + Math.sqrt(25) + "<br>";
txt = txt + "Math.sqrt(50) = " + Math.sqrt(50) + "<br>";
txt = txt + "Math.sqrt(100) = " + Math.sqrt(100) + "<br>";
txt = txt + "Math.sqrt(-1) = " + Math.sqrt(-1) + "<br>";

The output (value of txt) after running above script will be:

Math.sqrt(25) = 5
Math.sqrt(50) = 7.0710678118654755
Math.sqrt(100) = 10
Math.sqrt(-1) = NaN

❮ JavaScript - Math Object