Math.sqrt() Method in JavaScript: A Method to Get the Square Root

How do we use the Math.sqrt() method to get the square root of a number in JavaScript?

In this article, you’ll learn how to calculate square roots with ease and boost your coding skills.

Aside from that, you’ll find out how to square a number in JavaScript. Read on to explore more!

Let’s get started to find out how to calculate the square root of a number in JavaScript using the Math.sqrt() method.

What is Math.sqrt in JavaScript?

The Math.sqrt() is a built-in static method in JavaScript that is used to calculate the square root of a number.

The term “static” means that the method belongs to the Math object itself, and not to an instance of the Math object.

That’s the reason why we call it using Math.sqrt() and not on a Math object that we created.

What is math sqrt syntax?

Here’s the syntax of Math,sqrt() method:

Math.sqrt(number) ✅

What is the Parameter?

This function takes one parameter:

number

This is the number you want to find the square root of. It should be a non-negative number.

What is the return value?

The Math.sqrt() method returns the square root of the number provided. If the number is negative, it returns NaN (Not a Number).

Supported Browser

✔ Chrome

✔ Edge

✔ Firefox

✔ Internet Explorer

✔ Safari

✔ Opera

How does Math.sqrt () method work?

The sqrt() method short for “square root”, is a mathematical method that returns the square root of a given number.

In other words, it finds a value that, when multiplied by itself, gives the original number.

Here’s a simple example: the square root of 49 is 7 because 7 * 7 equals 49.

In JavaScript, you can use the Math.sqrt() method to calculate the square root of a number.

Here’s how it works:

let samplenumber = 64;
console.log(Math.sqrt(samplenumber));

Output:

8

If you try to find the square root of a negative number, the function will return NaN, which stands for “Not a Number,” because square roots of negative numbers are not real numbers.

Here’s an example:

let samplenumber = -64;
console.log(Math.sqrt(samplenumber));

Output:

NaN

Here are the other examples:

Math.sqrt(-81); // NaN
Math.sqrt(-0); // -0
Math.sqrt(0); // 0
Math.sqrt(1); // 1
Math.sqrt(2); // 1.414213562373095
Math.sqrt(16); // 4
Math.sqrt(Infinity); // Infinity

Frequently Asked Question (FAQs)

How to square in JavaScript?

You can square a number in JavaScript using the exponentiation operator (**).

Here’s an example:

let samplenum = 9;
let square = samplenum ** 2; ✅
console.log(square);

Output:

81

In our example, 9 ** 2 returns 81, because 9 * 9 is equal to 81.

What is the square root of 2 in JavaScript?

You can calculate the square root of a number in JavaScript using the Math.sqrt() method.

Here’s how you can find the square root of 2:

let samplenumber = 2;
console.log(Math.sqrt(samplenumber)); ✅

Output:

1.4142135623730951

As you can see in our example Math.sqrt(2) returns approximately 1.4142135623730951, because that is the square root of 2.

Conclusion

We’ve explored the Math.sqrt() method in JavaScript, which is used for calculating the square root of a number.

We’ve discussed its syntax, parameters, and return values and provided various examples.

Also, we illustrate how to square a number in JavaScript and calculate the square root of 2.

Please note that the Math.sqrt() is a static method, meaning it belongs to the Math object itself and not to an instance of the Math object.

It returns NaN for negative numbers as they do not have real square roots.

We are hoping that this article provides you with enough information that help you understand the Math.sqrt() method in JavaScript.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Leave a Comment