
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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.
- Related Articles
- How to get the square root of 2 in JavaScript?
- Get square root of a number using Math.sqrt in Java
- 8086 program to find the square root of a perfect square root number
- How to calculate square root of a number in Python?
- Finding square root of a number without using Math.sqrt() in JavaScript
- Returning a range or a number that specifies the square root of a number in JavaScript
- PHP – How to get the square root of an arbitrary precision number using bcsqrt() function?
- Finding square root of a number without using library functions - JavaScript
- How to calculate the nth root of a number in JavaScript?
- How to find the cube root of a number in JavaScript?
- Find the smallest number by which 1458 should be multiplied so as to get a perfect square. Also, find the square root of the square number obtained.
- Find the smallest number by which 180 should be multiplied so as to get a perfect square. Also, find the square root of the square number obtained.
- Finding square root of a non-negative number without using Math.sqrt() JavaScript
- 8085 program to find square root of a number
- 8086 program to find Square Root of a number
