mozilla
Your Search Results

    Math.hypot()

    This is an experimental technology, part of the ECMAScript 6 (Harmony) proposal.
    Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

    Summary

    The Math.hypot() function returns the square root of the sum of squares of its arguments, that is

    Math.hypot(v1,v2,,vn)=i=1nvi2=v12+v22++vn2\mathtt{\operatorname{Math.hypot}(v_1, v_2, \dots, v_n)} = \sqrt{\sum_{i=1}^n v_i^2} = \sqrt{v_1^2 + v_2^2 + \dots + v_n^2}

    Syntax

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

    Parameters

    value1, value2, ...
    Numbers.

    Description

    Because hypot() is a static method of Math, you always use it as Math.hypot(), rather than as a method of a Math object you created (Math is not a constructor).

    If no arguments are given, the result is +0.

    If at least one of arguments cannot be converted to a number, the result is NaN.

    With one argument, Math.hypot() returns the same as Math.abs().

    Examples

    Example: Using Math.hypot()

    Math.hypot(3, 4);        // 5
    Math.hypot(3, 4, 5);     // 7.0710678118654755
    Math.hypot();            // 0
    Math.hypot(NaN);         // NaN
    Math.hypot(3, 4, 'foo'); // NaN, +'foo' => NaN
    Math.hypot(3, 4, '5');   // 7.0710678118654755, +'5' => 5
    Math.hypot(-3);          // 3, the same as Math.abs(-3)
    

    Polyfill

    This can be emulated using the following function:

    Math.hypot = Math.hypot || function() {
      var y = 0;
      var length = arguments.length;
    
      for (var i = 0; i < length; i++) {
        if (arguments[i] === Infinity || arguments[i] === -Infinity) {
          return Infinity;
        }
        y += arguments[i] * arguments[i];
      }
      return Math.sqrt(y);
    };
    

    Specifications

    Specification Status Comment
    ECMAScript 6 (ECMA-262)
    The definition of 'Math.hypot' in that specification.
    Draft Initial definition.

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Basic support 38 27 (27) Not supported 25 7.1
    Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Basic support Not supported Not supported 27.0 (27) Not supported Not supported iOS 8

    See also

    Document Tags and Contributors

    Contributors to this page: Mingun, fscholz, ziyunfei, rwaldron, fred.wang, kchapelier, realityking
    Last updated by: Mingun,