Welcome folks today in this blog post we will be building a program to calculate the square root of number using math.sqrt() method
in javascript. All the full source code of the application is given below.
Get Started
In order to get started create a index.html
file and copy paste the code which is shown below
index.html
1 2 3 4 5 6 7 8 9 10 11 |
const number1 = 2.25; const number2 = -4; const number3 = 'hello'; const result1 = Math.sqrt(number1); const result2 = Math.sqrt(number2); const result3 = Math.sqrt(number3); console.log(`The square root of ${number1} is ${result1}`); console.log(`The square root of ${number2} is ${result2}`); console.log(`The square root of ${number3} is ${result3}`); |
Now if you open index.html
file inside the browser you will see the below result
User Input of Numbers Example
Now we will be taking the number as input
by the user
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!DOCTYPE html> <html> <head> <title>Add Two Numbers in Javascript</title> </head> <body> </body> <script> // take the input from the user const number = prompt('Enter the number: '); const result = Math.sqrt(number); alert(`The Square Root of ${number} is ${result}`) </script> </html> |