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:
Math.hypot()
This static-method returns 0 since no arguments are provided.
Returns value of type Number
.
Math.hypot(value1)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value1 | required | The 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
.
Math.hypot(value1, value2)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value1 | required | The first number to include in the sum of squares. |
value2 | required | The 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
.
Math.hypot(value1, value2, /* …, */ valueN)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value1 | required | The first number to include in the sum of squares. |
value2 | required | The second number to include in the sum of squares. |
valueN | optional | Additional 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,
- We call the
Math.hypot()
method with no arguments. - The result is stored in the variable
result
. - We log
result
to the console using theconsole.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,
- We call the
Math.hypot()
method with one argument3
. - The result is stored in the variable
result
. - We log
result
to the console using theconsole.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,
- We call the
Math.hypot()
method with arguments3
and4
. - The result is stored in the variable
result
. - We log
result
to the console using theconsole.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.