JavaScript Math.hypot()
Syntax & Examples


Math.hypot() static-method

The Math.hypot() static method in JavaScript returns the square root of the sum of squares of its arguments. It is often used to compute the Euclidean distance.


Syntax of Math.hypot()

There are 4 variations for the syntax of Math.hypot() static-method. They are:

1.
Math.hypot()

This static-method returns 0 since no arguments are provided.

Returns value of type Number.

2.
Math.hypot(value1)

Parameters

ParameterOptional/RequiredDescription
value1requiredThe first number to include in the sum of squares.

This static-method returns the square root of the square of value1.

Returns value of type Number.

3.
Math.hypot(value1, value2)

Parameters

ParameterOptional/RequiredDescription
value1requiredThe first number to include in the sum of squares.
value2requiredThe second number to include in the sum of squares.

This static-method returns the square root of the sum of squares of value1 and value2.

Returns value of type Number.

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

Parameters

ParameterOptional/RequiredDescription
value1requiredThe first number to include in the sum of squares.
value2requiredThe second number to include in the sum of squares.
valueNoptionalAdditional numbers to include in the sum of squares.

This static-method returns the square root of the sum of squares of value1, value2, and additional arguments valueN.

Returns value of type Number.



✐ Examples

1 Using Math.hypot() with no arguments

In JavaScript, if no arguments are provided to the Math.hypot() method, it returns 0.

For example,

  1. We call the Math.hypot() method with no arguments.
  2. The result is stored in the variable result.
  3. We log result to the console using the console.log() method.

JavaScript Program

const result = Math.hypot();
console.log(result);

Output

0

2 Using Math.hypot() with one argument

In JavaScript, we can use the Math.hypot() method to compute the square root of the sum of squares of a single number, which is the number itself.

For example,

  1. We call the Math.hypot() method with one argument 3.
  2. The result is stored in the variable result.
  3. We log result to the console using the console.log() method.

JavaScript Program

const result = Math.hypot(3);
console.log(result);

Output

3

3 Using Math.hypot() with multiple arguments

In JavaScript, we can use the Math.hypot() method to compute the square root of the sum of squares of multiple numbers.

For example,

  1. We call the Math.hypot() method with arguments 3 and 4.
  2. The result is stored in the variable result.
  3. We log result to the console using the console.log() method.

JavaScript Program

const result = Math.hypot(3, 4);
console.log(result);

Output

5

Summary

In this JavaScript tutorial, we learned about hypot() static-method of Math: the syntax and few working examples with output and detailed explanation for each example.