JavaScript Math.sqrt() solves the need to get square roots fast. Before it, you wrote custom code or used loops.
Table of Content
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)
number
must 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.