How to use Math.sqrt() Method In Javascript

The Math.sqrt() method is used to find the Square Root of a given number.

Syntax:

Math.sqrt( value )

Example: In this example we will find the square root using JavaScript Math.sqrt() method.

Javascript
const num1 = 25;
const num2 = -120;
const num3 = 'Geeks';

// Math.sqrt() function returns the
// square root of number
console.log('Square Root of ' + 
    num1 + ' is ' + Math.sqrt(num1));
  
// Math.sqrt() function returns NaN, because
// the passed value is negative integer
console.log('Square Root of ' + 
    num2 + ' is ' + Math.sqrt(num2));
    
// Math.sqrt() function returns NaN, because
// the passed value is string
console.log('Square Root of ' + 
    num3 + ' is ' + Math.sqrt(num3));

Output
Square Root of 25 is 5
Square Root of -120 is NaN
Square Root of Geeks is NaN

JavaScript Program to Find the Square Root

Given a number N, the task is to calculate the square root of a given number using JavaScript. There are the approaches to calculate the square root of the given number, these are:

Approaches to Find the Square Root of Given Number in JavaScript:

Table of Content

  • Using Math.sqrt() Method
  • Using JavaScript Math.pow() Method
  • Using Binary Search
  • Newton’s Method

Basic Examples:

Input: num = 25
Output: 2
Explanation: The square root of 25 is 5.
Input: num = -120
Output: NaN
Explanation: The square root of Negative numbers is NaN.
Input: num = 'Geeks'
Output: NaN
Explanation: The square root of String Value is NaN.

Similar Reads

Using Math.sqrt() Method

The Math.sqrt() method is used to find the Square Root of a given number....

Using JavaScript Math.pow() Method

The Math.pow() method is used calculate the power a number i.e., the value of the number raised to some exponent. To get the square root of any Number, we set the power to 1/2 of that number....

Using Binary Search

The binary search is used to find the square root of the given number N with precision upto 5 decimal places....

Newton’s Method

Newton’s method, also known as the Newton-Raphson method, is an iterative method for finding the roots of a real-valued function. To find the square root of a number using Newton’s method:...