How to use Math.pow() Method In Javascript
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.
Syntax:
Math.pow( num, 1/2 )
Example: In this example, we will calculate the square root of given values using Math.pow() method.
const num1 = 25;
const num2 = -120;
const num3 = 'Geeks';
// Math.pow() function returns the
// square root of number
console.log('Square Root of ' +
num1 + ' is ' + Math.pow(num1, 1/2));
// Math.pow() function returns NaN, because
// the passed value is negative integer
console.log('Square Root of ' +
num2 + ' is ' + Math.pow(num2, 1/2));
// Math.pow() function returns NaN, because
// the passed value is string
console.log('Square Root of ' +
num3 + ' is ' + Math.pow(num3, 1/2));
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.
Contact Us