The Math.hypot() method returns the square root of the sum of squares of its arguments.
Example
hypot() Syntax
The syntax of the hypot() method is:
Math.hypot(n1, n2, ..., nx)
Here, hypot() is a static method. Hence, we need to access the method using the class name, Math.
hypot() Parameters
The hypot() method takes in arbitrary (one or more) numbers as arguments.
- n1 - a number
- n2 - a number, and so on.
hypot() Return Value
The hypot() method returns:
- the square root of the sum of squares of the given arguments.
NaNif any argument is non-numeric.- 0 if no arguments are given.
Example 1: JavaScript Math.hypot()
In the above example,
Math.hypot(0.7, 2.4)computes the square root of the sum of squares of 0.7 and 2.4Math.hypot(7, 24, 25)computes the square root of the sum of squares of 7, 24, and 25
Example 2: hypot() With a Single Argument
In the above example, we have used Math.hypot() to compute the square root of the sum of squares of -3.
The output 3 indicates that for single-value arguments, their absolute values are returned.
Example 3: hypot() With No Arguments
In the above example, we have used Math.hypot() to compute the square root without passing any argument.
The output indicates that 0 is returned if we don't pass any argument.
Recommended Readings: