Math.sqrt()

Returns the square root of a number, or NaN when the number is negative.

Signature
Math.sqrt(x)
  • Added in JavaScript ES1

Parameters

ParameterTypeDescription
$xnumberA number greater than or equal to 0. Non-numbers are coerced first.

Return value

numberThe square root of x as a non-negative number. Returns NaN if x is negative, -0 if x is -0, and Infinity if x is Infinity.

Math.sqrt(x) returns the unique non-negative y where y * y === x. It is exactly Math.pow(x, 0.5) but clearer and, in most engines, faster.

When to reach for it

Geometry and statistics. Euclidean distance (Math.sqrt(dx * dx + dy * dy)), vector magnitude, standard deviation from a variance, and any Pythagorean calculation all bottom out here.

One optimisation worth knowing: if you only need to compare distances — nearest point, inside-a-radius test — compare the squared values and skip the square root entirely. It is the same ordering with less work and no rounding step.

What trips people up

A negative argument returns NaN, not an error. If x is computed rather than literal, a tiny floating-point undershoot below zero — common when subtracting nearly equal values in a variance calculation — silently produces NaN. Guard with Math.sqrt(Math.max(0, x)).

Results are not exact. Math.sqrt(2) is the nearest double to √2, and squaring it does not return exactly 2, so never test Math.sqrt(n) ** 2 === n. Use a tolerance.

For two-dimensional magnitudes specifically, Math.hypot(dx, dy) avoids intermediate overflow and underflow that Math.sqrt(dx * dx + dy * dy) can hit with extreme values.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Computing the distance between two points on a plane
  • Deriving standard deviation from a variance
  • Finding the magnitude of a 2D or 3D vector
  • Solving a quadratic with the discriminant

Frequently asked questions

How do I get a square root in JavaScript?

Call Math.sqrt(x). It is equivalent to x ** 0.5 but clearer and typically faster; for negative x it returns NaN.

Why does Math.sqrt() return NaN?

Because the argument is negative or coerces to NaN — the square root of a negative number is not a real number. Wrap computed values in Math.max(0, x) if floating-point error might push them slightly below zero.

How do I calculate the distance between two points?

Use Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2), or Math.hypot(x2 - x1, y2 - y1) which avoids overflow with very large coordinates. If you only need to compare distances, compare the squared values and skip the root.

More examples

Standard deviation, guarded against negative variance

The Math.max(0, variance) guard matters: with identical inputs the summed squares can land a hair below zero through floating-point error, and Math.sqrt would then return NaN instead of 0.

Example · javascript
function stdDev(values) {
  const mean = values.reduce((a, b) => a + b, 0) / values.length;
  const variance =
    values.reduce((a, v) => a + (v - mean) ** 2, 0) / values.length;
  return Math.sqrt(Math.max(0, variance));
}

console.log(stdDev([2, 4, 4, 4, 5, 5, 7, 9])); // 2
console.log(stdDev([5, 5, 5]).toFixed(1));     // "0.0"
console.log(stdDev([1, 100]));                 // 49.5

Skip the square root when only comparing

Squaring is monotonic for non-negative values, so ranking by squared distance gives the same answer as ranking by distance. [3, 4] is exactly on the radius and counts as inside; [6, 8] at distance 10 does not.

Example · javascript
const points = [[3, 4], [1, 1], [6, 8]];
const radius = 5;

// Compare squared distances — same ordering, no sqrt
const inside = points.filter(([x, y]) => x * x + y * y <= radius ** 2);
console.log(inside); // [[3, 4], [1, 1]]

const nearest = points.reduce((a, b) =>
  a[0] ** 2 + a[1] ** 2 <= b[0] ** 2 + b[1] ** 2 ? a : b
);
console.log(nearest); // [1, 1]

Discussion

  • Be the first to comment on this lesson.