JavaScript square root of Number | Example code

  • by

Use The Math sqrt() function to the square root of Number in JavaScript. This function returns the square root of a number.

Math.sqrt(x)

Note: The square root of the negative number is NaN returned. And If a string is passed, NaN is returned.

Example get square root in JavaScript

Simple example code gets the square root of 9.

<!DOCTYPE html>
<html>
<head>

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

    console.log(x); 
  </script>
</head>
<body>

</body>
</html>

Output:

JavaScript square root of Number

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

Do comment if you have any doubts or suggestions on this JS mathematical operation code.

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.

This site uses Akismet to reduce spam. Learn how your comment data is processed.