JavaScript | Math.hypot( ) Function

The Math.hypot() function in JavaScript is used to calculate the square root of the sum of squares of numbers passed to it as arguments.

It is basically used to find the hypotenuse of a right-angled triangle or the magnitude of a complex number. Math.hypot() function uses the formula Math.sqrt(v1*v1 + v2*v2) where v1 and v2 are either the sides of the triangle, or the real and complex values.

The hypot() is a static method of Math and therefore it is always used as Math.hypot() and not as a method of a Math object created.

Syntax:

Math.hypot(value1, value2,....)

Parameters: The Math.hypot() function accepts a list of numbers as parameters separated by comma ‘,’ operator. In the above syntax, value1, value2 are values which the user wants to send to the hypot() function.



Return Value: The Math.hypot() function returns he square root of the sum of squares of the passed arguments. It returns NaN if at least one of the arguments cannot be converted to a number.

Below programs illustrates the Math.hypot() function in JavaScript:

  1. When two positive numbers are passed as parameters:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
       document.write(Math.hypot(3, 4));          
    </script>

    chevron_right

    
    

    Output:

    5
  2. When two negative numbers are passed as parameters:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
       document.write(Math.hypot(-3, -4));          
    </script>

    chevron_right

    
    

    Output:

    5
  3. When more than two numbers are passed as parameters:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script type="text/javascript">
       document.write(Math.hypot(3, 6, 7)); 
    </script>

    chevron_right

    
    

    Output:

    9.695359714832659


  4. My Personal Notes arrow_drop_up

    I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.