Skip to content

JavaScript square root | Program

  • by

A simple way to find the square root of a number use the Math sqrt() function in JavaScript. It is built-in takes a number as a parameter and returns its square root.

Math.sqrt(number);

Note: The square root of the number is NaN if the number is Negative or String.

JavaScript square root

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>
   let a = Math.sqrt(0);
   let b = Math.sqrt(1);
   let c = Math.sqrt(9);
   let d = Math.sqrt(64);
   let e = Math.sqrt(-9); 
   console.log(a,b,c,d,e);
 </script>
</body>
</html>

Output:

JavaScript square root Program

Find Square Root of a Number by user input number

// take the input from the user
const n= prompt('Enter the number: ');

const res = Math.sqrt(number);
console.log(`The square root of ${n} is ${res}`);

Output:

Enter the number: 9 
The square root of 9 is 3

Do comment if you have any doubts or suggestions on this JS Math sqrt() method topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Tags:

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading