Javascript Reference - JavaScript sqrt() Method








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

Browser Support

sqrt() Yes Yes Yes Yes Yes

Syntax

Math.sqrt(x);

Parameter Values

Parameter Description
x Required. A number




Return Value

the square root of a number.

If x is a negative number, NaN is returned.

Example

The following code shows how to calculate the square root of a number.

<!--from   ww w.  j  av  a2 s  .c o  m-->
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
  function doMath() {
    var inputNum1 = document.form1.input1.value;
    var result = Math.sqrt(inputNum1);
    document.form1.answer.value = result;
  }
</script>
</head>
<body>
  <form name=form1>
    Enter Number: <input type="text" name="input1" size=10> <input
      type="button" value="Calculate" onClick='doMath()'> <br>
    The square root is: <input type="text" name="answer" size=20>
  </form>
</body>
</html>

Click to view the demo