Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math.min.apply(0, array) - why?

Tags:

javascript

I was just digging through some JavaScript code (Raphaël.js) and came across the following line (translated slightly):

Math.min.apply(0, x)

where x is an array. Why on earth would you do this? The behavior seems to be "take the min from the array x."

like image 909
Trevor Burnham Avatar asked May 19 '10 22:05

Trevor Burnham


People also ask

Can you use math min on an array?

To find the minimum value present in a given array, we can use the Math. min() function in JavaScript. This function returns the minimum value present in a given array. For example, let's define an array with some random values and find its minimum value using the Math.

Why is math MAX () less than math MIN ()?

max() starts with a search value of -Infinity , because any other number is going to be greater than -Infinity. Similarly, Math. min() starts with the search value of Infinity : “If no arguments are given, the result is Infinity .

What does apply () do in JavaScript?

Summary. The apply() method invokes a function with a given this value and arguments provided as an array. The apply() method is similar to the call() method excepts that it accepts the arguments of the function as an array instead of individual arguments.

How does math min work in JavaScript?

The min() function returns the smallest value from the numbers provided. If no parameters are provided, the min() function will return Infinity. If any of the parameters provided are not numbers, the min() function will return NaN.


3 Answers

I realized the answer as I was posting my own question: This is the most succinct way of taking the min of an array x in JavaScript. The first argument is totally arbitrary; I find the 0 confusing because the code intuitively means "Take the min of 0 and x," which is absolutely not the case. Using the Math object makes more sense for human-readability, but the Raphael.js authors are obsessed with minification and 0 is three bytes shorter.

See http://ejohn.org/blog/fast-javascript-maxmin/

For readability's sake, I'd strongly urge people to stop doing this and instead define a function along the lines of

function arrayMin(arr) { return Math.min.apply(Math, arr); };
like image 184
Trevor Burnham Avatar answered Oct 14 '22 16:10

Trevor Burnham


The reason is this:

  • Your input x is an array

  • The signature of Math.min() doesn't take arrays, only comma separated arguments

  • If you were using Function.prototype.call() it would have almost the same signature, except the first argument is the this context, i.e. who's "calling"

    • Example: Math.min.call(context, num1, num2, num3)
  • The context only matters when you refer to this inside the function, e.g. most methods you can call on an array: Array.prototype.<method> would refer to this (the array to the left of the 'dot') inside the method.

  • Function.prototype.apply() is very similar to .call(), only that instead of taking comma-separated arguments, it now takes an array after the context.

    • Function.prototype.call(context, arg1, arg2, arg3)
    • Function.prototype.apply(context, [arg1, arg2, arg3])
  • The 0 or null that you put in as the first argument is just a place shifter.

like image 25
David Rosson Avatar answered Oct 14 '22 16:10

David Rosson


The proper and less confusing way to call this is: Math.min.apply(null, array)

If parameter is unused setting it to null makes code more readable than setting it to 0 or Math

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

like image 36
Igor Vaschuk Avatar answered Oct 14 '22 15:10

Igor Vaschuk