basic
Math.hypot
Returns the square root of the sum of squares of its arguments — the Euclidean norm. Introduced in ES2015 and safer than manual sqrt(a*a + b*b) against overflow.
Math.hypot(...values) Parameters & edge cases
| Case | Behavior |
|---|---|
| ...values | Any number of numeric arguments (0 or more) |
| no args | Math.hypot() returns 0 |
| overflow-safe | Handles very large/small values without intermediate overflow to Infinity |
Examples
console.log(Math.hypot(3, 4)); // 5 Classic 3-4-5 Pythagorean triangle.
console.log(Math.hypot(3, 4, 12)); // 13 Works in n dimensions — here, 3D distance from origin.
console.log(Math.hypot()); // 0 Zero arguments give 0 (an empty vector's norm).
console.log(Math.hypot(-3, 4)); // 5 Signs don't matter — squares eliminate them.
Gotcha
In hot loops, Math.hypot is measurably slower than manual `Math.sqrt(a*a + b*b)` because it inspects for overflow — use the manual form only when you know values stay in a safe range.
Related
Math.sqrt
Returns the positive square root of a number. Equivalent to Math.pow(x, 0.5) but faster and clearer.
Math.pow
Returns base raised to the power of exponent. In modern code the `**` operator (ES2016) is usually preferred for readability and identical semantics.
Math.abs
Returns the absolute value of a number — its distance from zero without regard to sign. Useful for differences, magnitudes, and tolerance checks.
Math.cbrt
Returns the cube root of a number. Introduced in ES2015, and unlike sqrt it handles negative inputs correctly.