Tutorialspoint.dev

JavaScript | Math.sqrt( ) function

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

Math.sqrt(value)
Parameters :
The number whose square root
is to be calculated.
Returns :
Square root of the number passed as parameter.
passed as parameter.

Example :

Input  : Math.sqrt(4)
Output : 2

Input  : Math.sqrt(-4)
Output : NaN

Errors and Exceptions
1. A non-numeric string passed as parameter returns NaN
2. An array with more than 1 integer passed as parameter returns NaN
3. A negative number passed as parameter returns NaN
4. An empty string passed as parameter returns NaN
5. An empty array passed as parameter returns NaN

Below are some examples that illustrate the Math.floor() function in JavaScript:

<!-- NEGATIVE NUMBER EXAMPLE -->
<script type="text/javascript">
    document.write(Math.sqrt(-2)); 
    document.write(Math.sqrt(-2.56));          
</script>

Output:

br>
NaN
NaN
<!-- POSITIVE NUMBER EXAMPLE -->
<script type="text/javascript">
    document.write(Math.sqrt(2)); 
    document.write(Math.sqrt(2.56));          
</script>

Output:

1.4142135623730951
1.6
<!-- STRING EXAMPLE -->
<script type="text/javascript">
    document.write(Math.floor("Geeksforgeeks"));          
</script>

Output:

NaN
<!-- ADDITION INSIDE FUNCTION EXAMPLE -->
<script type="text/javascript">
    document.write(Math.floor(7.2+9.3));           
</script>

Output:

4.06201920231798


This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter