Home > Article > Web Front-end > How to perform root calculation in javascript
In JavaScript, you can use the sqrt() method to perform root calculations. This method can return the square root of a specified number. The syntax is "Math.sqrt(value)". When the value in the function is a negative number, The result returned is "NaN".
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
The sqrt() method can return the square root of a number.
Syntax
Math.sqrt(x)
Parameter Description
x Required. Must be a number greater than or equal to 0.
Return value
Number/NaN 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>菜鸟教程(runoob.com)</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:The above is the detailed content of How to perform root calculation in javascript. For more information, please follow other related articles on the PHP Chinese website!