JavaScript Program to Find the Square Root

Before we start writing code to find the square root of a number, first we need to understand the concept of square root.

JavaScript Program to Find the Square Root

A Square Root is a number that, when multiplied by itself, results in the original number. It's also known as the inverse of the exponent of 2.

For example, the square root of 4 is 2 and the square root of 100 is 10.

Square roots can be negative and positive. The square root of 0 is undefined because no value has an infinite number of factors and only one power of 0.

Prerequisites

Square Root of a Number

We will be using the Math.sqrt() method to find the square root of a number.

Math.sqrt(number);

In the above code,

// prompt the use to enter a number
const number = prompt('Enter a number: ');

// find the square root of the number
const result = Math.sqrt(number);

// print the result (which is the square root of the number)
console.log(`The square root of ${number} is ${result}`);

The above code will prompt the user to enter a number and then find the square root of that number.

Enter the number: 100
The square root of 100 is 10

Square Root of Different Data Types

JavaScript has a number of data types that can be used. We will try to find the square root of the following types of data:

const data1 = 10.04;
const data2 = -16;
const data3 = 'Hello, world!';
const data4 = true;
const data5 = false;

const result1 = Math.sqrt(data1);
console.log(`The square root of ${data1} is ${result1}`);
// The square root of 10.04 is 3.1685959035509716

const result2 = Math.sqrt(data2);
console.log(`The square root of ${data2} is ${result2}`);
// The square root of -16 is NaN

const result3 = Math.sqrt(data3);
console.log(`The square root of ${data3} is ${result3}`);
// The square root of Hello, world! is NaN

const result4 = Math.sqrt(data4);
console.log(`The square root of ${data4} is ${result4}`);
// The square root of true is 1

const result4 = Math.sqrt(data5);
console.log(`The square root of ${data4} is ${result4}`);
// The square root of false is 0

Things to remember about Math.sqrt() function

Summary

In this tutorial, we have learnt how to find the square root of a number using JavaScript. We learnt about finding the square root of different data types and we also looked into the JavaScript's built-in Math.sqrt() method to find the square root of a number.

Please checkout our related content section below for more tutorials on similar topics.


We hope that you have learnt something from our tutorials. Please feel free to share this content with your friends.


If you have any questions or want some other content to be added to our website so that it can help you learn and grow further, please do not hesitate to contact us at info@createmysite.org. We would love to hear from you.

We are hiring!! If you are tech blogger who loves writing content on modern technologies, we would love to get in touch with you. Just drop us a mail at the email address mentioned above and we will reach out to you as soon as possible.

If you are interested in learning more about our website, please visit our about page.