Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Math.hypot: Euclidean length

Math.hypot converts any number of components to Number and stably computes the square root of their sum of squares.

Version: ECMAScript 2015 onward; example target: Node.js 24.22.2

Syntax

Math.hypot(...values)

Math.hypot(...values) returns √(x₁² + x₂² + …) as a non-negative Number. With no arguments it returns +0, and component signs do not affect the length. Its scaled algorithm avoids premature overflow and underflow better than direct Math.sqrt(x*x + y*y).

Every argument undergoes ToNumber in order. A Symbol or BigInt throws TypeError even if another argument is infinite. After successful conversion, any ±Infinity makes the result Infinity, even alongside NaN; without infinity, any NaN yields NaN. Numeric strings are converted and objects can run conversion code.

The function is not a constructor and has been standard since ECMAScript 2015. Use it for vector magnitude or distance such as Math.hypot(x2-x1, y2-y1), with any number of dimensions. The result is a Number approximation; range stability does not remove ordinary IEEE 754 precision limits.

Examples

console.log(Math.hypot(3, 4), Math.hypot());
console.log(Object.is(Math.hypot(-0), 0));
const stable = Math.hypot(3e200, 4e200);
const naive = Math.sqrt((3e200) ** 2 + (4e200) ** 2);
console.log(stable, Number.isFinite(stable), naive);
console.log(Math.hypot(Infinity, NaN), Number.isNaN(Math.hypot(NaN, 1)));
try { Math.hypot(Infinity, 1n); } catch (error) { console.log(error instanceof TypeError); }

Result

5 0
true
4.9999999999999995e+200 true Infinity
Infinity true
true

Sources