Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Math
  6. hypot()

Math.hypot()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

TheMath.hypot() static method returns the square root of the sum of squares of its arguments. That is,

𝙼𝚊𝚝𝚑.𝚑𝚢𝚙𝚘𝚝(v1,v2,,vn)=i=1nvi2=v12+v22++vn2\mathtt{\operatorname{Math.hypot}(v_1, v_2, \dots, v_n)} = \sqrt{\sum_{i=1}^n v_i^2} = \sqrt{v_1^2 + v_2^2 + \dots + v_n^2}

Try it

console.log(Math.hypot(3, 4));// Expected output: 5console.log(Math.hypot(5, 12));// Expected output: 13console.log(Math.hypot(3, 4, 5));// Expected output: 7.0710678118654755console.log(Math.hypot(-5));// Expected output: 5

Syntax

js
Math.hypot()Math.hypot(value1)Math.hypot(value1, value2)Math.hypot(value1, value2, /* …, */ valueN)

Parameters

value1, …,valueN

Numbers.

Return value

The square root of the sum of squares of the given arguments. ReturnsInfinity if any of the arguments is ±Infinity. Otherwise, if at least one of the arguments is or is converted toNaN, returnsNaN. Returns0 if no arguments are given or all arguments are ±0.

Description

Calculating the hypotenuse of a right triangle, or the magnitude of a complex number, uses the formulaMath.sqrt(v1*v1 + v2*v2), where v1 and v2 are the lengths of the triangle's legs, or the complex number's real and complex components. The corresponding distance in 2 or more dimensions can be calculated by adding more squares under the square root:Math.sqrt(v1*v1 + v2*v2 + v3*v3 + v4*v4).

This function makes this calculation easier and faster; you callMath.hypot(v1, v2), orMath.hypot(v1, /* …, */, vN).

Math.hypot also avoids overflow/underflow problems if the magnitude of your numbers is very large. The largest number you can represent in JS isNumber.MAX_VALUE, which is around 10308. If your numbers are larger than about 10154, taking the square of them will result in Infinity. For example,Math.sqrt(1e200*1e200 + 1e200*1e200) = Infinity. If you usehypot() instead, you get a better answer:Math.hypot(1e200, 1e200) = 1.4142...e+200. This is also true with very small numbers.Math.sqrt(1e-200*1e-200 + 1e-200*1e-200) = 0, butMath.hypot(1e-200, 1e-200) = 1.4142...e-200.

With one argument,Math.hypot() is equivalent toMath.abs().Math.hypot.length is 2, which weakly signals that it's designed to handle at least two parameters.

Becausehypot() is a static method ofMath, you always use it asMath.hypot(), rather than as a method of aMath object you created (Math is not a constructor).

Examples

Using Math.hypot()

js
Math.hypot(3, 4); // 5Math.hypot(3, 4, 5); // 7.0710678118654755Math.hypot(); // 0Math.hypot(NaN); // NaNMath.hypot(NaN, Infinity); // InfinityMath.hypot(3, 4, "foo"); // NaN, since +'foo' => NaNMath.hypot(3, 4, "5"); // 7.0710678118654755, +'5' => 5Math.hypot(-3); // 3, the same as Math.abs(-3)

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-math.hypot

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp