Below is the example of the Math sqrt() Method.
- Example:
<script type="text/javascript">document.write(Math.sqrt(-2) +"<br>");document.write(Math.sqrt(-2.56));</script>chevron_rightfilter_none - Output:
NaN NaN
The Math.sqrt() method in JavaScript is used to square root of the number passed as parameter to the function.
Syntax
Math.sqrt(value)
Parameters: This method accepts a single parameter as mentioned above and described below:
- value which hold the number whose square root is to be calculated.
Returns: Square root of the number passed as parameter passed as parameter.
Below example illustrate the Mathe sqrt() method in JavaScript:
Input : Math.sqrt(4) Output : 2
Input : Math.sqrt(-4) Output : NaN
Errors and Exceptions:
- A non-numeric string passed as parameter returns NaN
- An array with more than 1 integer passed as parameter returns NaN
- A negative number passed as parameter returns NaN
- An empty string passed as parameter returns NaN
- An empty array passed as parameter returns NaN
More codes for the above method are as follows:
Program 1:
<script type="text/javascript"> document.write(Math.sqrt(2) + "<br>"); document.write(Math.sqrt(2.56)); </script> |
Output:
1.4142135623730951 1.6
Program 2:
<script type="text/javascript"> document.write(Math.floor("Geeksforgeeks")); </script> |
Output:
NaN
Program 3:
<script type="text/javascript"> document.write(Math.floor(7.2+9.3)); </script> |
Output:
4.06201920231798
Supported Browsers:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- Find all Factors of Large Perfect Square Natural Number in O(sqrt(sqrt(N))
- PHP Math Functions (is_nan, pow, sqrt, exp, log, log10, log1p, max, min, getrandmax, rand, mt_rand)
- PHP | sqrt( ) Function
- p5.js | sqrt() function
- Square root of a number without using sqrt() function
- Floor square root without using sqrt() function : Recursive
- JavaScript Math cosh() Method
- JavaScript Math random() Method
- JavaScript Math round( ) Method
- JavaScript Math abs( ) Method
- JavaScript Math floor() Method
- JavaScript Math pow( ) Method
- JavaScript Math trunc( ) Method
- JavaScript Math exp( ) Method
- JavaScript Math log( ) Method
- JavaScript Math cos( ) Method
- JavaScript Math tan( ) Method
- JavaScript Math ceil( ) Method
- JavaScript Math sin( ) Method
- JavaScript Math min( ) Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


