EcmaScript 6 – Math.hypot()

Many know that JavaScript features vary from browser to browser. In the case of Firefox, there are many new features that have been proposed in EcmaScript 6. One of them is Math.hypot. This function adds up the squares of each argument passed and returns the square-root of that sum. In other words it could be used to find the length of the hypotenuse. Unfortunately, if you want to use this function it is not yet available in most other browsers. That is where the following snippet comes into play:

Math.hypot = Math.hypot || function() {
  var val, ret = 0, args = arguments, i = args.length;
  while (i--) {
    ret += (val = +args[i]) * val;
  }
  return Math.sqrt(ret);
};

Since there are a number of these newly proposed functions, I plan on writing a post to define a safe fallback for each one. Happy coding! 8-)

Leave a Reply

Your email address will not be published. Required fields are marked *


one × = 1

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="">