What is the use of Math.hypot() function in JavaScript?

JavascriptObject Oriented ProgrammingFront End Technology

Math.hypot()

Math.Hypot() method is used to find the square root of the sum of the squares of the elements that are passed to it as arguments. This method is actually used to find the hypotenuse of a right-angle triangle whose sides are passed as arguments into it. 

syntax

Math.hypot(arg1, arg2,....);

Example

In the following example, sides of a right-angle triangle are passed to find the hypotenuse. If any value is can't be converted to a number then NaN will be displayed as output. 

Live Demo

<html>
<body>
<script>
   document.write(Math.hypot(7, 24));
   document.write("</br>");
   document.write(Math.hypot(7, "hi"));
</script>
</body>
</html>

Output

25
NaN


This method even accepts negative values as arguments and tries to give the square root of some of their squares as output.

Example

Live Demo

<html>
<body>
<script>
   document.write(Math.hypot(-7, -24));
   document.write("</br>")
   document.write(Math.hypot(-3, -4))
</script>
</body>
</html>

Output

25
5


This method can take multiple values, more than two, and tries to give the square root of some of their squares.

Example

Live Demo

<html>
<body>
<script>
   document.write(Math.hypot(1, 2, 3));
</script>
</body>
</html>

Output

3.74165738677
raja
Published on 09-Jul-2019 18:25:22
Advertisements