JavaScript Program to Find the Square Root

To understand this example, you should have the knowledge of the following JavaScript programming topics:


To find the square root of a number in JavaScript, you can use the built-in Math.sqrt() method. Its syntax is:

Math.sqrt(number);

Here, the Math.sqrt() method takes a number and returns its square root.


Example: Square Root of a Number

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

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

Output

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

Example 2: Square Root of Different Data Types

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}`);

Output

The square root of 2.25 is 1.5
The square root of -4 is NaN
The square root of hello is NaN
  • If 0 or a positive number is passed in the Math.sqrt() method, then the square root of that number is returned.
  • If a negative number is passed, NaN is returned.
  • If a string is passed, NaN is returned.

Also Read:

Before we wrap up, let’s put your knowledge of JavaScript Program to Find the Square Root to the test! Can you solve the following challenge?

Challenge:

Write a function to find the square root of a number.

  • Return the square root of the given number num.
  • For example, if num = 25, the expected output is 5.
Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community