Skip to content
Related Articles

Related Articles

JavaScript Math sqrt( ) Method

Improve Article
Save Article
Like Article
  • Last Updated : 17 Feb, 2022

Below is the example of the Math sqrt() Method. 
 

  • Example: 
     

javascript




<script type="text/javascript">
    document.write(Math.sqrt(-2) + "<br>"); 
    document.write(Math.sqrt(-2.56));          
</script>


  • 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 Math sqrt() method in JavaScript:
 

  • Example 1: 
     
Input : Math.sqrt(4)
Output : 2

 

  • Example 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: 
 

javascript




<script type="text/javascript">
    document.write(Math.sqrt(2) + "<br>"); 
    document.write(Math.sqrt(2.56));          
</script>


Output: 
 

1.4142135623730951
1.6

Program 2: 
 

javascript




<script type="text/javascript">
    document.write(Math.floor("Geeksforgeeks"));          
</script>


Output: 
 

NaN

Program 3: 
 

javascript




<script type="text/javascript">
    document.write(Math.floor(7.2+9.3));           
</script>


Output: 
 

4.06201920231798

Supported Browsers: 
 

  • Google Chrome 1 and above
  • Internet Explorer 3 and above
  • Firefox 1 and above
  • Opera 3 and above
  • Safari 1 and above

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!