JavaScript Math.sqrt() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

Math.sqrt(x) returns the square root of a number. For example, Math.sqrt(9) is 3 because 3 × 3 = 9.

Read the syntax below, then try the three examples to see the method in action.

Syntax

Call the method on Math. Start with this line.

JavaScript
Math.sqrt(x)
CallWhat it does
Math.sqrt(x)Returns the square root of x.

Built into JavaScript. Call Math.sqrt(...) on the Math object — no import required. Math is not a constructor, so never write new Math().

Library declarations
JavaScript
Math.sqrt(x)

Parameters

ParameterDescription
xRequired. A number that is 0 or more.

Return Value

Return ValueDescription
NumberThe square root of x. Returns NaN if x is negative or not a number.

JavaScript Examples

Example 1 — Basic Square Roots

Perfect squares give whole numbers. Other numbers give long decimals.

JavaScript
console.log(Math.sqrt(9)); // 3
console.log(Math.sqrt(2)); // 1.4142135623730951
console.log(Math.sqrt(0)); // 0
Try It Yourself

How It Works

  1. Perfect square. 9 is 3 × 3, so the result is 3.
  2. Not a perfect square. Math.sqrt(2) is a long decimal.
  3. Zero. The square root of 0 is 0.
  4. Rounding. Use toFixed(2) if you want a short number.

Example 2 — Negative Numbers Return NaN

You cannot take the real square root of a negative number.

JavaScript
console.log(Math.sqrt(-1)); // NaN
console.log(Number.isNaN(Math.sqrt(-4))); // true
console.log(Math.sqrt(Infinity)); // Infinity
console.log(Math.sqrt("16")); // 4
Try It Yourself

How It Works

  1. No real answer. Math.sqrt(-1) is NaN.
  2. Check for NaN. Number.isNaN() returns true for NaN.
  3. Infinity. The square root of Infinity is Infinity.
  4. Text numbers. "16" is converted to 16, so the result is 4.

Example 3 — Distance with Pythagoras

Find the straight-line distance between two points using Math.sqrt(). Then check for whole-number roots.

JavaScript
const dx = 3;
const dy = 4;
const distance = Math.sqrt(dx * dx + dy * dy);
console.log(distance); // 5

console.log(Number.isInteger(Math.sqrt(49))); // true
console.log(Number.isInteger(Math.sqrt(50))); // false
Try It Yourself

How It Works

  1. Square the sides. 3 * 3 + 4 * 4 is 25.
  2. Take the root. Math.sqrt(25) is 5, the length of the long side.
  3. Perfect square check. Math.sqrt(49) is 7, a whole number, so it prints true.
  4. Not perfect. Math.sqrt(50) is a decimal, so it prints false.

Browser & Runtime Support

Math.sqrt is part of the first JavaScript standard (ES1) and is Baseline Widely available in every browser and in Node.js.

✓ Baseline · ES1

Math.sqrt

Supported in Chrome, Firefox, Safari, Edge, Internet Explorer and Node.js for many years. No polyfill needed.

99% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Math.sqrt Excellent

Bottom line: Use Math.sqrt anywhere. It works in all browsers.

Frequently Asked Questions

It returns the square root of a number. Math.sqrt(16) is 4 because 4 x 4 = 16.
It returns NaN, because a negative number has no real square root.
Use Number.isInteger(Math.sqrt(n)). It is true for 49 and false for 50.
They give the same answer for normal numbers. Math.sqrt() is clearer and slightly easier to read.
Use Math.cbrt(). For example, Math.cbrt(27) is 3.
All of them. It is part of the first JavaScript standard and works in every browser and in Node.js.

Next: sumPrecise()

Learn how to add numbers with fewer rounding errors.

sumPrecise() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful