➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
If a variable or value is a number, Math.sqrt() returns its square root. To demonstrate the syntax of this static function, find the square root of 25 and log it into the console.
The output for the above code is 5. Thus the syntax for the square root function can be stated as:
Case 1: Square root of array (single value)
Math.sqrt() returns the square root of an array with a single element if the given value is an array with a single element.
Case 2: Square root of an array (multiple values)
If the value or argument supplied is an array with several elements, Math.sqrt() will return NaN.
console.log(Math.sqrt([4, 9, 16]));
Case 3: Non-numeric value
In the scenario where the value supplied is not a valid number, Math.sqrt() will return NaN.
Case 4: Square root of a negative number
If a negative value is supplied as an argument to Math.sqrt(), the result will always be NaN.
Case 5: Outputs 0 when:
This will always return 0 if we give in an empty array, an empty string, or null.
console.log(Math.sqrt(null)); console.log(Math.sqrt()); console.log(Math.sqrt(''));
If we pass in an empty object, the result will be NaN.
Case 7: Arithmetic operations and square roots
It’s critical to understand that we can use Math.sqrt() to execute acceptable arithmetic operations. This will return as long as the resulting value is legitimate.
console.log(Math.sqrt(50 + 50)); console.log(Math.sqrt(-2 + -2)); let x = 50; console.log(Math.sqrt(50 + x)); console.log(Math.sqrt(-2 + -x));
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!