In this article, you’ll learn how to calculate square roots with ease and boost your coding skills.
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:
What is the Parameter?
This function takes one parameter:
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).
✔ Internet Explorer
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.
Here’s how it works:
let samplenumber = 64; console.log(Math.sqrt(samplenumber)); ✅
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)); ✅
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)
Here’s an example:
let samplenum = 9; let square = samplenum ** 2; ✅ console.log(square);
In our example, 9 ** 2 returns 81, because 9 * 9 is equal to 81.
Here’s how you can find the square root of 2:
let samplenumber = 2; console.log(Math.sqrt(samplenumber)); ✅
As you can see in our example Math.sqrt(2) returns approximately 1.4142135623730951, because that is the square root of 2.
We’ve discussed its syntax, parameters, and return values and provided various examples.
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.
Thank you for reading Itsourcecoders 😊.