To find the square root of a number in JavaScript, you can use the built-in Math.sqrt() method. Its syntax is:
Math.sqrt(number);
Here, the Math.sqrt() method takes a number and returns its square root.
Example: Square Root of a Number
Output
Enter the number: 9 The square root of 9 is 3
Example 2: Square Root of Different Data Types
Output
The square root of 2.25 is 1.5 The square root of -4 is NaN The square root of hello is NaN
- If 0 or a positive number is passed in the
Math.sqrt()method, then the square root of that number is returned. - If a negative number is passed,
NaNis returned. - If a string is passed,
NaNis returned.