Math sqrt() Method - Javascript Math

Javascript examples for Math:sqrt

Description

The sqrt() method returns the square root of a number.

Parameter Values

Parameter Description
x Required. A number

Return Value:

A Number. If x is a negative number, NaN is returned

The following code shows how to return the square root of a number:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*  w  w  w.java2 s. c om*/
    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 = a + "<br>" + b + "<br>" + c + "<br>" + d + "<br>" + e;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials