Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Math sqrt() Method

JavaScript Math sqrt() Method

The JavaScript Math sqrt( ) Method in JavaScript is used to square the root of the number passed as a parameter to the function. 

Syntax:

Math.sqrt(value)

Parameters: This method accepts a single parameter as mentioned above and described below: 

  • Value: which holds the number whose square root is to be calculated.

Returns: Square root of the number passed as a parameter.

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

Example: 

javascript




<script type="text/javascript">
    console.log(Math.sqrt(-2));
    console.log(Math.sqrt(-2.56));        
</script>


Output: 

NaN
NaN 

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 a parameter returns NaN
  • An array with more than 1 integer passed as a parameter returns NaN
  • A negative number passed as a parameter returns NaN
  • An empty string passed as a parameter returns NaN
  • An empty array passed as a parameter returns NaN

More codes for the above method are as follows: 

Program 1: 

javascript




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


Output: 

1.4142135623730951
1.6

Program 2: 

javascript




<script type="text/javascript">
    console.log(Math.floor("Geeksforneveropen"));        
</script>


Output: 

NaN

Program 3: 

javascript




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


Output: 

4.06201920231798

We have a complete list of Javascript Math Objects methods, to check those please go through this Javascript Math Object Complete reference article.

Supported Browsers: 

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

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments