JavaScript Math sqrt()

The sqrt() method computes the square root of a specified number and returns it.

Example


sqrt() Syntax

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

Math.sqrt(number)

Here, sqrt() is a static method. Hence, we are accessing the method using the class name, Math.


sqrt() Parameter

The sqrt() method takes a single parameter:

  • number - value whose square root is to be calculated

sqrt() Return Value

The sqrt() method returns:

  • the square root of a given positive integer or decimal number
  • NaN (Not a Number) if the argument is non-numeric or negative

Example 1: JavaScript Math.sqrt()

Here, we have used the Math.sqrt() method to compute the square root of an integer value, 16 and a decimal value, 144.64.


Example 2: sqrt() with Negative Argument

Mathematically, the square root of any negative number is an imaginary number. That is why the sqrt() method returns NaN as the output.


Example 3: sqrt() with Infinity Values


Example 4: sqrt() with Numeric String

In the above example, the Math.sqrt() method converts the numeric string "81" into a number and then computes its square root.


Example 5: sqrt() with Non-Numeric Argument

In the above example, we have tried to calculate the square root of the string "Harry". That's why we get NaN as the output.


Recommended readings: