JavaScript

How to calculate the square root of a number in JavaScript

In this tutorial, we are going to see how to calculate the square root of a number using the Math.sqrt() method. This method returns the square root of a number. If the value of a number is negative, sqrt returns NaN(Not a Number).
 

Script to calculate the square root of a number in JavaScript :
<html>
   <head>
      <title>Calculate the square root of a number in JS</title>
   </head>
   <body>
      <script>
         var res = Math.sqrt( 1.5 );
         document.write("First value : " + res + "<br />");

         var res = Math.sqrt( 16 );
         document.write("Second value : " + res );
      </script>
   </body>
</html>

Output:

First value : 1.224744871391589
Second value : 4
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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