When I type in an array into the parameter of the javascript math minimum and maximum functions, it returns the correct value:
console.log( Math.min( 5 ) );
When given no arguments, Math.min()
equals infinity and Math.max()
is -infinity
.
This is probably to ensure that any value is smaller than the minimum found so far, and larger than the maximum so far.
Theoretically the outcome of those functions is can't be given. The Ecma specification dictates the outcome of the Min and Max functions without arguments (See page 163).
Obviously you can have all sort of arguments about what the outcome should be, but there isn't a strictly correct answer anyway. I guess Ecma choose this because it's the easiest to implement. Normally a max function works roughly like this
result = -infinity;
foreach(arg in arguments)
if(arg > result)
result = arg;
As you can see, returning -infinity when the function is called without arguments requires no changes at all.
[ECMA-262: 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 −∞.
- If any value is
NaN
, the result isNaN
.- 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.
The
length
property of themax
method is 2.
[ECMA-262: 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 +∞.
- If any value is
NaN
, the result isNaN
.- 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.
The
length
property of themin
method is 2.
With no arguments, Math.min
is a value you can use to calculate an iterative minimum, not a physical minimum value for the type. It does this by being the opposite: a physical maximum value for the type. (Similarly in the other direction for Math.max
; and clearly +∞ < -∞
is false
.)
i.e.
var a = [1,2,3,4,5];
var x = Math.min();
for (var i = 0; i < a.length; i++) {
if (a[i] < x) { // will succeed on first iteration
// because `x` is initialised to highest possible value
x = a[i];
}
}
(In fact, it may simply be that the standard is making implementation of Math.min
easier, since it probably initialises its result to +Infinity before doing its work on any argument present, using an algorithm similar to the above.)
Of course, this example is slightly contrived since we could just write:
var x = Math.min(a[0], a[1], a[2], a[3], a[4]);
However, the loop is useful if we don't know the number of elements in the array, since the variant of Math.min
you're using that accepts an array is non-standard.
Even then, you can do:
var x = Math.min.apply(null, a);
// ^ reflective function-call
// ^ no object instance; function is "static"
// ^ array of arguments
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