JavaScript Math sqrt()

The JavaScript Math.sqrt() function returns the square root of a specified number.

The syntax of the Math.sqrt() function is:

Math.sqrt(x)

sqrt(), being a static method, is called using the Math class name.


Math.sqrt() Parameters

The Math.sqrt() function takes in:

  • x - A number

Return value from Math.sqrt()

  • Returns the positive square root of a given number.
  • Returns NaN if the given number is negative.

Example: Using Math.sqrt()

// Using Math.sqrt()
var value = Math.sqrt(16);
console.log(value); // 4

var value = Math.sqrt(2);
console.log(value); // 1.4142135623730951

var value = Math.sqrt(0);
console.log(value); // 0

// returns NaN for negative numbers
var value = Math.sqrt(-2);
console.log(value); // NaN

Output

4
1.4142135623730951
0
NaN

Recommended readings: