References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript Math.hypot() method

Method JavaScript All modern browsers Updated
Quick answer

The Math.hypot() method returns the square root of the sum of the squares of its arguments — the length of the hypotenuse. Math.hypot(3, 4) is 5. It's the clean, accurate way to compute the straight-line distance between two points, replacing manual Math.sqrt() of squared differences.

Overview

Math.hypot() computes the square root of the sum of squares of all its arguments — exactly the Pythagorean calculation. Math.hypot(3, 4) is 5, and it takes any number of arguments, so it works for 2D, 3D and beyond: Math.hypot(x, y, z).

Its purpose is the distance between two points. Instead of writing Math.sqrt(dx*dx + dy*dy) by hand, you write Math.hypot(dx, dy) — shorter, clearer, and it also avoids intermediate overflow or underflow for very large or very small values, so it's more numerically robust than the manual version.

It's used constantly in games, graphics and UI for proximity and collision checks — how far is the cursor from a target, are two elements within a threshold. If you only need to compare distances (which is bigger), comparing the squared distances (dx*dx + dy*dy) skips the square root and is a touch faster; use Math.hypot() when you need the actual distance value.

Syntax

Math.hypot(a, b)        // sqrt(a*a + b*b)

Math.hypot(3, 4)        // 5
Math.hypot(dx, dy)      // distance between two points
Math.hypot(x, y, z)     // 3D distance

Parameters

The Math.hypot() method accepts the following parameters.

Parameter Description
value1 ... valueN The numbers to combine. Returns the square root of the sum of their squares.

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  const a = { x: 1, y: 2 }, b = { x: 4, y: 6 };

  const dist = Math.hypot(b.x - a.x, b.y - a.y);

  document.getElementById('out').textContent =
    'hypot(3, 4): ' + Math.hypot(3, 4) + '\n' +
    'distance:    ' + dist; // hypot(3, 4): 5 / distance: 5
</script>

Best practices

  • Use Math.hypot(dx, dy) for the distance between two points — clearer and more robust than manual Math.sqrt().
  • It accepts any number of arguments, so it works in 2D, 3D and higher.
  • For comparing which distance is larger, compare squared distances to skip the square root.
  • Pair the deltas with subtraction: Math.hypot(x2 - x1, y2 - y1).

Frequently asked questions

What does Math.hypot() do?
It returns the square root of the sum of the squares of its arguments — the hypotenuse, e.g. Math.hypot(3, 4) is 5.
How do I calculate the distance between two points?
Use Math.hypot(x2 - x1, y2 - y1).
What is the difference between Math.hypot() and Math.sqrt()?
Math.sqrt() takes one number; Math.hypot() squares and sums several arguments first, so it directly gives the distance and avoids intermediate overflow.
Does Math.hypot() work in 3D?
Yes — pass three arguments: Math.hypot(x, y, z).