Home > Article > Web Front-end > How to find the square root in JavaScript
In JavaScript, you can use the sqrt() method to achieve the effect of finding the square root. This method is used to return the square root of a number. The parameter value in the method must be a number, and when the parameter in the method is less than zero When , the returned result is "NaN", and the syntax is "Math.sqrt (the value requiring the square root)".
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
You can use the sqrt() method to find the square root.
The sqrt() method returns the square root of a number.
The syntax is:
Math.sqrt(x)
Parameter x is required. Must be a number greater than or equal to 0.
The returned result is: the square root of parameter x. If x is less than 0, returns NaN.
The example is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>123</title> </head> <body> <p id="demo">单击按钮显示不同数值的平方根。</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var a=Math.sqrt(0); var b=Math.sqrt(1); var c=Math.sqrt(9); var d=Math.sqrt(64); var e=Math.sqrt(-9); var x=document.getElementById("demo") x.innerHTML=a + "<br>" + b + "<br>" + c + "<br>" + d + "<br>" + e; } </script> </body> </html>
Output result:
[Related recommendations: javascript video tutorial, webfrontend】
The above is the detailed content of How to find the square root in JavaScript. For more information, please follow other related articles on the PHP Chinese website!