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.
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
- Basic understanding of JavaScript
- JavaScript data types
- JavaScript variables and constants
- JavaScript Math.sqrt() function
- JavaScript operators (
+
and=
operators in this case)
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,
- Math is the object that contains the
sqrt()
method - The
sqrt()
method returns the square root of the number - And
number
is the number whose square root is to be found
// 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:
- a fraction,
- a negative number,
- a string, and
- a boolean
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
- If a number is zero, the
sqrt()
method returns the square root of the number - If a number is positive, the
sqrt()
method returns the square root of the number - If a number is negative, the
sqrt()
method returnsNaN
- If a number is a string, the
sqrt()
method returnsNaN
- If a number is a boolean, the
sqrt()
method returns 1 or 0 depnding on the value of the boolean whether it istrue
orfalse
respectively
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.
Related Content
- Learn more about JavaScript
- Learn more about JavaScript Arrays
- JavaScript Program to Add Two Numbers
- JavaScript Program to Calculate the Area of a Triangle
- JavaScript Program to Convert Date to a Number
- JavaScript Program to Find the Square Root of a Number
- JavaScript Program to Pass a Function as a Parameter
- JavaScript Program to Remove all Whitespaces From a Text
- JavaScript Program to Write Console Statements
- JavaScript Hello World Program
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.