Home  >  Article  >  Web Front-end  >  How to perform root calculation in javascript

How to perform root calculation in javascript

WBOY
WBOYOriginal
2022-02-14 16:42:041880browse

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

How to perform root calculation in javascript

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

How to perform root calculation in javascript

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:

How to perform root calculation in javascript

##Related recommendations:

javascript learning tutorial

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!

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