Program to find the Square Root in JavaScript

The blog provides the step by step solution to find the square root of the given number. The square root is applicable for the numeric number. If string is passed to calculate square root, NaN (Not a Number) is returned.

Steps to write square root program
  • Prompt user to enter the number
  • Use Math.sqrt(number) to calculate the square root of the given number
let X = prompt('Enter the first number: ');
let Y = prompt('Enter the second number: ');

let output1 ;
let output2;

document.writeln(`Value for X ': ${X}` +"<br>");
document.writeln(`Value for Y ': ${Y}` +"<br>");

output1 = Math.sqrt(X);
output2 = Math.sqrt(Y);

document.writeln("----Calcualte Square Root--------"  +"<br>");
document.writeln(`Square Root  for X ': ${output1}` +"<br>");
document.writeln(`Square Root  for Y ': ${output2}` +"<br>");



Value for X ': 5
Value for Y ': -21
----Calcualte Square Root--------
Square Root for X ': 2.23606797749979
Square Root for Y ': NaN