How to get the square root of a number in JavaScript?


In this tutorial, we will learn how to get the square root of a number in JavaScript in both ways.

The square root of a number in JavaScript can be found in two ways −

  • Using Math.sqrt() Method
  • By creating a Custom Function

The square root of a number is a factor that, when multiplied by itself, gives the original number. For example, 10 is the square root of 100 because if you multiply 10 by itself (10*10), the result will be 100.

Using the Math.sqrt() Method

In JavaScript, The Math.sqrt() method is useful for finding the square root of a number. It takes a variable as a parameter and returns the square root value if the variable is a positive number. Otherwise, if the variable is a negative number or a non-numeric value, then it returns NaN which denotes “Not a Number”.

Users can follow the syntax below to find a number's square root using the Math.sqrt() method.

Syntax

Math.sqrt( number );

Parameters

  • number − it takes any variable which is a number whose square root needs to be found out.

Return Type

  • square root of a given positive number.

  • NaN if the argument is a negative number or a non-numeric value.

Example 1

In the below example, we have used the Math.sqrt() method to find the square root of a number. We have taken the different values to observe the output of the Math.sqrt() method.

<html> <body> <h4> Get the square root of a number using <i>Math.sqrt() </i> method </h4> <div id="root"> </div> <script> let positive_number = 4; let negative_number = -4; let root = document.getElementById('root'); root.innerHTML = "The square root of " + positive_number + " is: " + Math.sqrt(positive_number) + "<br>"; root.innerHTML += "The square root of " + negative_number + " is:" + Math.sqrt(negative_number) + "<br>"; </script> </body> </html>

In the above output, users can see that Math.sqrt() method returns the desired square root value for the positive integer, and for the negative and non-numeric values, it returns NaN.

By Creating a Custom Method

The square root of a number can also find out without using Math.sqrt() method and in this approach, we need to calculate the square root value using iteration.

Syntax

function square_root(num) {
   //checking if number is negative or non-numeric
   if (num < 0 || isNaN(num)) {
      return NaN
   }
   
   //starting the calculation from half of the number
   let square_root = num / 2
   let temp = 0
 
   // Iterating while square_root is not equal to temp
   while (square_root != temp) {
      temp = square_root
 
      // smalling the square_root value to find square root
      square_root = (num / square_root + square_root) / 2
   }

 return square_root
}

Algorithm

Step 1 − Declare a function and take a number as an argument in the function.

Step 2 − Check if the number is negative or non-numeric and return NaN..

Step 3 − Setup square_root variable as half of the given number and declare and also a temp variable with value 0.

Step 4 − Iterating while temp is not equal to square_root value.

Step 4.1 − Storing the square_root value in temp.

Step 4.2 − number is divided by the current square_root value and also adds the result of the current square_root value with it.

Step 5 − Return the square_root value.

Example

In the below example, we find out the square root of a number without Math.sqrt() method. We have taken the different values to observe the output.

<html> <body> <h4> Get the square root of a number without using <i> custom </i> method. </h4> <div id = "root"> </div> <script> function square_root(num) { //checking if number is negative or non-numeric if (num < 0 || isNaN(num)) { return NaN } //starting the calculation from half of the number let square_root = num / 2 let temp = 0 // Iterating while square_root is not equal to temp while (square_root != temp) { temp = square_root // smalling the square_root value to find the square root square_root = (num / square_root + square_root) / 2 } return square_root } let positive_number = 121; let negative_number = -6; let root = document.getElementById('root'); root.innerHTML = "The square root of " + positive_number + " is: " + square_root(positive_number) + "<br>";root.innerHTML += "The square root of " + negative_number + " is: " +square_root(negative_number) + "<br>"; </script> </body> </html>

In the above output, users can see that the square_root method returns the desired square root value for the positive integer and the negative and non-numeric values. We have learned how to get the square root value of a number using JavaScript with and without Math.sqrt() method. The first approach is the standard approach for getting the square root value. The second method is a more controllable way of doing the same and if you want to do more than just getting the square root value, go for the second approach. It is recommended to use the Math.sqrt() method as it is a built-in method of JavaScript.

Updated on: 31-Oct-2022

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements