Table of Content
JavaScript Math.sqrt() solves the need to get square roots fast. Before it, you wrote custom code or used loops.
It saves time and avoids errors. It works the same every time. JavaScript Math sqrt now gives all browsers the same method.
How to Use Math.sqrt() in JavaScript
JavaScript Math.sqrt() finds the square root of a number. It checks the input. If it finds a valid number, it returns the square root. If the number is negative, it returns NaN. You cannot use it on strings or other data types.
Use this syntax when you write the code:
Math.sqrt(number)numbermust be a numeric value.- It returns the square root if valid.
- It returns NaN if the number is negative.
What this means is simple. JavaScript checks the number. It finds the value that, when multiplied by itself, gives the input.
Use it when you need to find square roots fast. It helps in math problems and finance.
Here’s how it works in practice:
Math.sqrt(25)This returns the expected value. It checks 25. The square root of 25 is 5. It gives 5 as output. This avoids loops. It avoids custom logic. You get a clean result with one line. You can use this anywhere that needs a square root.
Examples of JavaScript Math.sqrt()
1. Find square root of 9
Math.sqrt(9)This gives the result 3. It checks 9. Then it finds the number that gives 9 when squared. That number is 3. It returns 3.
2. Find square root of 0
Math.sqrt(0)This gives the result 0. It checks 0. The square root of 0 is 0. That’s the result. It returns it directly.
3. Use decimal number
Math.sqrt(2.25)This gives the result 1.5. It checks 2.25. It finds 1.5 as the number that, squared, gives 2.25.
4. Use a negative number
Math.sqrt(-4)This gives the result NaN. It checks -4. Square roots of negative numbers are not real. So it cannot return a number. It returns NaN.
5. Use a variable as input
let x = 64;
Math.sqrt(x);This gives the result 8. It checks the value inside x, which is 64. It then returns the square root of 64. The result is 8.
6. Use with Math.pow to reverse it
Math.sqrt(Math.pow(7, 2))This gives the result 7. It first squares 7. That gives 49. Then it finds the square root of 49. That brings it back to 7.
Browser and JavaScript Version Support
Compatibility Across Browsers
- All modern browsers support Math.sqrt.
- It works on Chrome, Firefox, Safari, Edge, and Opera.
- It also works in mobile browsers.
Support in Older JavaScript Versions
- Math.sqrt works since the start of JavaScript.
- Even very old versions support it.
- You do not need any library or extra setup.
It runs without errors across all platforms. You can use it in any project, new or old.
Wrapping Up
In this article, you learned what JavaScript Math.sqrt() does.
Here’s a quick recap:
- Math.sqrt finds the square root.
- It takes one number as input.
- It returns the root if valid.
- It returns NaN if the number is negative.
- You can use variables inside it.
- It works on decimals too.
- It works in all browsers.
- It needs no extra setup.
- You can use it with other Math methods.
- It gives clean, simple results fast.
FAQs
What does Math.sqrt do in JavaScript?
Can Math.sqrt handle decimals?
What happens if I pass a string?
Is Math.sqrt supported in all browsers?
Can I use Math.sqrt with variables?
Similar Reads
Math.exp is a built-in JavaScript function that returns the value of e raised to a given number. Here, e is…
The Math.acos function finds the angle in radians from a cosine value in JavaScript. It gives a value between 0…
JavaScript runs code in different ways, but the for loop stays one of the most common tools. You use it…
JavaScript toReversed returns a new array with the reversed order of its elements. It does not change the original array…
JavaScript uses popup boxes to show quick messages or ask for direct input. These boxes stop the flow of the…
JavaScript Objects hold values as key and value pairs, and they help you store and access data. Understand the JavaScript…
JavaScript optional chaining lets you access object values without runtime errors. It checks if a property exists before access and…
AJAX keeps your web app fast. You do not need to reload the page every time you send or get…
JavaScript arithmetic operators let you add, subtract, multiply, or divide numbers. You can also use them to find remainders or…
Math.sin() in JavaScript gives the sine of a number. This number must be in radians. You use it to work…