The Math.hypot()
function returns the square root of the sum of squares of its arguments, that is
Math.hypot([value1[, value2[, ...]]])
value1, value2, ...
Because hypot()
is a static method of Math
, you always use it as Math.hypot()
, rather than as a method of a Math
object you created (Math
is not a constructor).
If no arguments are given, the result is +0.
If at least one of arguments cannot be converted to a number, the result is NaN.
With one argument, Math.hypot()
returns the same as Math.abs()
.
Math.hypot()
Math.hypot(3, 4); // 5 Math.hypot(3, 4, 5); // 7.0710678118654755 Math.hypot(); // 0 Math.hypot(NaN); // NaN Math.hypot(3, 4, 'foo'); // NaN, +'foo' => NaN Math.hypot(3, 4, '5'); // 7.0710678118654755, +'5' => 5 Math.hypot(-3); // 3, the same as Math.abs(-3)
This can be emulated using the following function:
Math.hypot = Math.hypot || function() { var y = 0; var length = arguments.length; for (var i = 0; i < length; i++) { if (arguments[i] === Infinity || arguments[i] === -Infinity) { return Infinity; } y += arguments[i] * arguments[i]; } return Math.sqrt(y); };
Created by Mozilla Contributors, license: CC-BY-SA 2.5