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.
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.