Home  >  Article  >  Web Front-end  >  How to find the square root in JavaScript

How to find the square root in JavaScript

WBOY
WBOYOriginal
2022-04-11 12:09:063277browse

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)".

How to find the square root in JavaScript

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

How to find the square root in JavaScript

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:

How to find the square root in JavaScript

[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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn