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.
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 is Math.max() less than Math.min()?
Applying Math.min() to an empty list produces -Infinity instead of 0
Interesting........Any explanations why Math.max return negative infinity?
Math.max and Math.min NaN on undefined entry
Videos
I assume the undefined is actually some variable.
You can substitute -Infinity for any NaN value to ensure a number.
var foo;
Math.max(5, 10, isNaN(foo) ? -Infinity : foo); // returns 10
Same concept can be used on Math.min, but with Infinity:
var foo;
Math.min(5, 10, isNaN(foo) ? Infinity : foo); // returns 5
Write your own max function:
function max() {
var par = []
for (var i = 0; i < arguments.length; i++) {
if (!isNaN(arguments[i])) {
par.push(arguments[i]);
}
}
return Math.max.apply(Math, par);
}
Or shorter using array filtering:
function max() {
var args = Array.prototype.slice.call(arguments);
return Math.max.apply(Math, args.filter(function(val) {
return !isNaN(val);
}));
}
Usage:
max(5, 10, undefined); // output: 10
What happens with Math.max([]) is that [] is first converted to a string and then to a number. It is not actually considered an array of arguments.
With Math.max(...[]) the array is considered a collection of arguments through the spread operator. Since the array is empty, this is the same as calling without arguments.
Which according to the docs produces -Infinity
If no arguments are given, the result is -Infinity.
Some examples to show the difference in calls with arrays:
console.log(+[]); //0 [] -> '' -> 0
console.log(+[3]); //3 [] -> '3' -> 3
console.log(+[3,4]); //Nan
console.log(...[3]); //3
console.log(...[3,4]); //3 4 (the array is used as arguments)
console.log(Math.max([])); //0 [] is converted to 0
console.log(Math.max()); // -infinity: default without arguments
console.log(Math.max(...[])); // -infinity
console.log(Math.max([3,4])); //Nan
console.log(Math.max(...[3,4])); //4
If you see the internal implementation documentation, you can tell why Math.max is returning -Infinity when there is no argument passed.
If no arguments are given, the result is -∞.
So when you spread an empty array in a function call, it is like calling the function without an argument.