JavaScript Math.hypot() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

Math.hypot(...values) returns the square root of the sum of the squares of its arguments. With two values, it gives the length of the longest side (hypotenuse) of a right triangle.

Math is built into JavaScript. Call the method directly. It is not a constructor, so never write new Math().

Syntax

Call Math.hypot() like this. Start with this line.

JavaScript
Math.hypot(value1, value2, /* …, */ valueN)
CallWhat it does
Math.hypot(value1, value2, ...)Returns the square root of the sum of the squares of all values.
Math.hypot()With no arguments, returns 0.

Built into JavaScript. Call Math.hypot() on the Math object — no import required.

Library declarations
JavaScript
Math.hypot(value1, value2, /* …, */ valueN)
JavaScript
interface Math {
  hypot(...values: number[]): number;
}

Parameters

ParameterDescription
value1, ..., valueNOptional. Any number of numbers. With no values, the result is 0.

Return Value

Return ValueDescription
numberThe square root of the sum of squares. Returns Infinity if any value is Infinity, and NaN if any value is NaN (and none is Infinity).

JavaScript Examples

Example 1 — Basic Use

Pass the sides of a right triangle. You get the longest side.

JavaScript
console.log(Math.hypot(3, 4));     // 5
console.log(Math.hypot(5, 12));    // 13
console.log(Math.hypot(-3, 4));    // 5
console.log(Math.hypot());         // 0
Try It Yourself

How It Works

  1. 3-4-5. 3² + 4² is 25, and its square root is 5.
  2. 5-12-13. Another well-known triangle gives 13.
  3. Negative values. Squares are always positive, so the signs do not matter.
  4. No values. Math.hypot() returns 0.

Example 2 — Distance Between Two Points

The straight-line distance is hypot(dx, dy).

JavaScript
var p1 = { x: 1, y: 2 };
var p2 = { x: 4, y: 6 };

var dx = p2.x - p1.x;
var dy = p2.y - p1.y;

console.log(Math.hypot(dx, dy));  // 5
Try It Yourself

How It Works

  1. Find the gaps. dx is 3 and dy is 4.
  2. Call hypot. Math.hypot(3, 4) is 5.
  3. Result. The two points are 5 units apart.

Example 3 — Many Values and Spread

Use more than two values, or spread an array into the call.

JavaScript
console.log(Math.hypot(1, 2, 2));        // 3

var vector = [2, 3, 6];
console.log(Math.hypot(...vector));      // 7

console.log(Math.hypot(Infinity, NaN));  // Infinity
console.log(Math.hypot(3, NaN));         // NaN
Try It Yourself

How It Works

  1. 3D length. 1² + 2² + 2² is 9, and the square root is 3.
  2. Spread. ...vector passes each array item as its own argument.
  3. Infinity wins. If any value is Infinity, the result is Infinity.
  4. NaN. Otherwise a NaN makes the result NaN.

Browser & Runtime Support

Math.hypot() is part of ES2015 and is Baseline Widely available in modern browsers and Node.js.

✓ Baseline · ES2015

Math.hypot()

Supported in Chrome, Firefox, Safari, Edge, and Node.js for years. Safe for modern apps without a polyfill.

98% 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.hypot Excellent

Bottom line: Use Math.hypot() in new code. Only very old browsers, like Internet Explorer, lack it.

Frequently Asked Questions

Math.hypot(a, b) returns the square root of a squared plus b squared. Math.hypot(3, 4) is 5.
Use Math.hypot(x2 - x1, y2 - y1). It works as the straight-line distance.
Yes. It accepts any number of arguments. Math.hypot(1, 2, 2) is 3.
It returns 0.
It is shorter, takes any number of values, and avoids overflow or underflow in the middle of the calculation.

Next: imul()

Learn how to multiply two 32-bit integers.

imul() 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.

11 people found this page helpful