To calculate the end root of a number, use Math.abs() method. Here, we’re using the Match.pow method also. You can try to run the following code to calculate the square root of numbers in JavaScript −
Example
<html> <head> <title>Calculate root</title> </head> <body> <script> function displayRoot(x, num) { val = num % 2; if((val == 1) || x < 0) x = -x; var a, num; a = Math.pow(x, 1 / num); num = Math.pow(a, num); if(Math.abs(x - num) < 1 && (x > 0 === num > 0)) return val ? -a : a; } document.write(displayRoot(36, 2)); document.write("<br>"+displayRoot(49, 2)); document.write("<br>"+displayRoot(36, -2)); document.write("<br>"+displayRoot(216, -2)); </script> -+ </body> </html>