JavaScript SQRT

The JavaScript SQRT function is a Math function used to find the square root of a specified expression or a specific number. The syntax of the JavaScript SQRT Function is:

Math.sqrt(number);
  • If the number argument is a positive integer, the SQRT function will return the square root of a given value.
  • If it is Negative or not a number, JavaScript Square root function will return NaN.
  • And if it is Null, the SQRT function returns Zero.

JavaScript SQRT Function Example

The JavaScript SQRT function finds the square root of a given number. In this example, we are going to find the JavaScript square root of different data types and display the output

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript SQRT Function </title>
</head>
<body>
  <h1> JavaScript SQRT Function </h1>
  <p id = "Pos"></p>
  <p id = "Neg"></p>
  <p id = "Dec"></p>
  <p id = "Neg_Dec"></p>
  <p id = "Str"></p>
  <p id = "Null"></p>
  <script>
    document.getElementById("Pos").innerHTML = Math.sqrt(10);
    document.getElementById("Neg").innerHTML = Math.sqrt(-10);
    document.getElementById("Dec").innerHTML = Math.sqrt(14.05);
    document.getElementById("Neg_Dec").innerHTML = Math.sqrt(-6.05);
    document.getElementById("Str").innerHTML = Math.sqrt("JavaScript");
    document.getElementById("Null").innerHTML = Math.sqrt(null);
  </script>
</body>
</html>

ANALYSIS

  1. First two statements, We used the SQRT Function directly on the Positive and negative integer. From the below screenshot, you can see the SQRT Function is returning NaN as output for negative integer
  2. Following four statements, we used the JavaScript SQRT Function on the Positive and negative Decimal values. As we said before, the SQRT Function is returning NaN for negative decimal.
  3. SQRT Function on the string value and this also returns NaN
  4. Last, We tried Js SQRT Math function on Null value, and it returns Zero as output
JavaScript SQRT Function