Why Math.min() > Math.max()?

Of course it would, because the start number should be Infinity for Math.min. All number that are lower than positive infinity should be the smallest from a list, if there are no smaller.

And for Math.max it's the same; all numbers that are larger than negative infinity should be the biggest if there are no bigger.

So for your first example:

Math.min(5) where 5 is smaller than positive infinity (Infinity) it will return 5.

Update

Calling Math.min() and Math.max with an array parameter may not work on every platform. You should do the following instead:

Math.min.apply(null, [ 1, 2, 3, 4 , 5 ]);

Where the first parameter is the scope argument. Because Math.min() and Math.max() are "static" functions, we should set the scope argument to null.


It's tricky, but important, to decide correctly what aggregate functions should do when passed the empty set.

Sometimes it's 'intuitively obvious': What is the SUM of no elements? Zero, I'm sure everyone would readily say.

Sometimes it's less so: What is the PRODUCT of no elements? Those with some mathematical training will quickly say "one", but this is not at all obvious.

Then you get to MIN and MAX and wow! How did we get those infinities?


One way to decide what an aggregate function should do here is consider what behaviours we want to remain consistent, even with empty sets. For example, suppose we have these non-empty sets:

A = { 1, 2, 3 } 
B = { 4, 5 }

Now, it's true here, and indeed for any non-empty sets, that

SUM(A ∪ B) = SUM({SUM(A), SUM(B)})
15 = 6 + 9

PRODUCT(A ∪ B) = PRODUCT({ PRODUCT(A), PRODUCT(B) })
120 = 6 * 20

MIN(A ∪ B) = MIN({ MIN(A), MIN(B) })
1 = MIN(1, 4)

Wouldn't it be nice, say the mathematicians, if these properties remain true even when one or both of the sets are empty? It surely would.

And it's maintaining this behaviour that decides what value we assign to SOME_AGGREGATE_FUNCTION(∅) :

In order for

SUM(A ∪ B) = SUM({ SUM(A), SUM(B) })

to remain true when A is empty and B is not, we must have SUM(∅) = 0

In order for

PRODUCT(A ∪ B) = PRODUCT({ PRODUCT(A), PRODUCT(B) })

to remain true when A is empty and B is not, we must have PRODUCT(∅) = 1

And finally:

In order for

MIN(A ∪ B) = MIN({ MIN(A), MIN(B) })

to remain true when A is empty and B is not, we need MIN(∅) to be a value which is guaranteed to be greater than any possible value in B, so that it doesn't 'interfere with' the result of MIN(B). And we get our answer: MIN(∅) = +∞


Why does it do this?

Because thats what the standard says should happen;

15.8.2.11 max ( [ value1 [ , value2 [ , … ] ] ] )

Given zero or more arguments, calls ToNumber on each of the arguments and returns the largest of the resulting values.

  • If no arguments are given, the result is -Infinity
  • If any value is NaN, the result is NaN.
  • The comparison of values to determine the largest value is done as in 11.8.5 except that +0 is considered to be larger than 0.

15.8.2.12 min ( [ value1 [ , value2 [ , … ] ] ] )

Given zero or more arguments, calls ToNumber on each of the arguments and returns the smallest of the resulting values.

  • If no arguments are given, the result is Infinity.
  • If any value is NaN, the result is NaN.
  • The comparison of values to determine the smallest value is done as in 11.8.5 except that +0 is considered to be larger than 0

p.s; It is non standard that Math.max() or Math.min() accepts an array. Use Math.max(a,b,c,d,e,...) etc instead.

In Chrome at least;

Math.max([1,2,3,4]); // NaN