C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Math sqrt() methodThe JavaScript math sqrt() method returns the square root of a number. If the provided number is negative, it returns NaN. SyntaxThe sqrt() method is represented by the following syntax: Math.sqrt(num) Parameternum - A number. ReturnThe square root of the given number. JavaScript Math sqrt() method exampleHere, we will understand sqrt() method through various examples. Example 1Let's see an example to print square root of the given numbers. <script> document.writeln(Math.sqrt(16)+ "<br>"); document.writeln(Math.sqrt(12)); </script> Output: 4 3.4641016151377544 Example 2Let's see some cases where sqrt() method returns NaN. <script> document.writeln(Math.sqrt()+ "<br>"); document.writeln(Math.sqrt(-9)); </script> Output: NaN NaN Example 3Here, you can test sqrt() method with your own test cases. <script> function display() { var x=document.getElementById("num").value; document.getElementById("result").innerHTML=Math.sqrt(x); } </script> <form> Enter a number: <input type="text" id="num"> <input type="button" onclick="display()" value="submit"> </form> <p><span id="result"></span></p>
Next TopicJavaScript Math
|