Skip to content

JavaScript Math sqrt() function | Get square root of a number

  • by

JavaScript sqrt() function is used to get the square root of a number. The sqrt() function is a static function of the Math object, it must be invoked through the placeholder object called Math.

The sqrt() function returns the square root of a number and its syntax:

Math.sqrt(number);

Note: If x is a negative number, NaN is returned.

JavaScript Math sqrt() example

A simple example code gets the square root of a 9.

<!DOCTYPE html>
<html>
<head>

  <script>
    let x = Math.sqrt(9);

    console.log(x);

  </script>

</head>

</html>

Output:

JavaScript Math sqrt() function

More Example

Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095

Math.sqrt(1);  // 1
Math.sqrt(0);  // 0
Math.sqrt(-1); // NaN
Math.sqrt(-0); // -0
  • A non-numeric string passed as a parameter returns NaN
  • An array with more than 1 integer passed as a parameter returns NaN
  • A negative number passed as a parameter returns NaN
  • An empty string passed as a parameter returns NaN
  • An empty array passed as a parameter returns NaN

Do comment if you have any doubts or suggestions on this JS sqrt() function.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *