This tutorial explains how to find the square root of a number in JavaScript. In JavaScript, we can use the in-built Math.sqrt()
function to find the square root of a number.
Syntax
The syntax for sqrt() function in JavaScript is:
Math.sqrt(number);
Here, number must be greater than or equal to 0. If the number is a negative number, the sqrt() function will return NaN.
Example
Let’s see an example on how to use the sqrt() function in JavaScript.
console.log(Math.sqrt(25));
console.log(Math.sqrt(5));
console.log(Math.sqrt(-9));
Output
5
2.2360679774997896
NaN