JavaScript Math hypot()

The JavaScript Math.hypot() function returns the square root of the sum of squares of its arguments.

It returns Math.sqrt(n1*n1 + n2*n2 + ... + nx*nx).

It represents the distance between points (n1, n2, ..., nx) and origin.

The syntax of the Math.hypot() function is:

Math.hypot(n1, n2, ..., nx)

hypot(), being a static method, is called using the Math class name.


Math.hypot() Parameters

The Math.hypot() function takes in arbitrary (one or more) numbers as arguments.


Return value from Math.hypot()

  • Returns the square root of the sum of squares of the given arguments.
  • Returns NaN if any argument is non-numeric.
  • Returns +0 if no arguments are given.

Example: Using Math.hypot()

var num = Math.hypot(3, 4);
console.log(num); // 5

var num = Math.hypot(7, 24, 25);
console.log(num); // 35.35533905932738

// single argument is equivalent to absolute value
var num = Math.hypot(-3);
console.log(num); // 3

var num = Math.hypot(3, 4, "5");
console.log(num); // 7.0710678118654755

// returns +0 for no argument
var num = Math.hypot();
console.log(num); // 0

Output

5
35.35533905932738
3
7.0710678118654755
0

Recommended readings: