Math.sqrt
Returns the square root of a number
Syntax
Math.sqrt(x)Parameters
| Parameter | Type | Description |
|---|---|---|
| x | number | A non-negative number |
Return Value
The square root of x, or NaN if x is negative
Examples
console.log(Math.sqrt(9)); // 3
console.log(Math.sqrt(2)); // 1.4142135623730951function distance(x1: number, y1: number, x2: number, y2: number) {
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
}
console.log(distance(0, 0, 3, 4)); // 5console.log(Math.sqrt(-1)); // NaN
console.log(Math.sqrt(0)); // 0Understanding Math.sqrt
The Math.sqrt method in JavaScript returns the square root of a number. It belongs to the Math object and is one of the most widely used methods for working with math values in modern JavaScript and TypeScript applications.
The method signature is Math.sqrt(x). It accepts 1 parameter: x. When called, it returns the square root of x, or nan if x is negative. Understanding when and how to use sqrt() helps you write more expressive, readable code.
Common use cases for Math.sqrt include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like math-cbrt, math-pow, math-hypot, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Math.sqrt is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
More Math Methods
Other methods in the Math object
Related Tools
More Math Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.