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.

Answer from KARASZI István on Stack Overflow
Top answer
1 of 10
50

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.

2 of 10
39

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(∅) = +∞

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › min
Math.min() - JavaScript - MDN Web Docs
The Math.min() static method returns the smallest of the numbers given as input parameters, or Infinity if there are no parameters.
Discussions

Why is Math.max() less than Math.min()?
I got this far in the article: > Math.max() Math.max() -Infinity > Math.min() Infinity So really it's just saying that negative infinity is less than positive infinity, which is true. And it makes sense that those are the 'default' values you get when you don't supply any arguments, as they're basically the respective identities for the two functions. Just like 0 is the additive identity and 1 is the multiplicative, -Infinity is the "maximizing" identity. For any number X, Math.max(-Infinity, X) is X. More on reddit.com
🌐 r/javascript
28
98
February 24, 2015
Applying Math.min() to an empty list produces -Infinity instead of 0
I have developed a code in which adds values and at the end subtracts the smallest value based on the items you select in a form. The code works fine however the problem occurs when you unselect al... More on stackoverflow.com
🌐 stackoverflow.com
April 16, 2013
Interesting........Any explanations why Math.max return negative infinity?
The maximum of zero arguments should be less than* the maximum of one or more than any other set of valid arguments, thus it is the lowest possible value; negative infinity. Min is the opposite of this logic. i.e. Math.max(...[]) < Math.max(...[1]); // true and Math.max(...[]) < Math.max(...[Number.MIN_SAFE_INTEGER]); //true makes sense. * I suppose you could less than or equal to if you include -Infinity itself More on reddit.com
🌐 r/learnjavascript
35
86
July 18, 2021
Math.max and Math.min NaN on undefined entry
When passing values to the Math.max or Math.min function in JavaScript, they return the highest and lowest values from the input respectively. However, if a piece of data that is undefined is entered, e.g. ... The result returned is NaN. Is there a simply way to fix this using JS/jQuery? ... You can write your own wrapper function for checking, or simply implement it yourself. ... I assume the undefined is actually some variable. You can substitute -Infinity ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
DEV Community
dev.to › dance2die › math-min-returns-infinity-1bi6
Math.min returns Infinity? - DEV Community
June 23, 2019 - Any value other than Infinity itself (Infinity === Infinity is true) should be the minimum, and as 3 is smaller than Infinity, 3 is returned by Math.min.
🌐
Reddit
reddit.com › r/javascript › why is math.max() less than math.min()?
r/javascript on Reddit: Why is Math.max() less than Math.min()?
February 24, 2015 - Just like 0 is the additive identity and 1 is the multiplicative, -Infinity is the "maximizing" identity. For any number X, Math.max(-Infinity, X) is X. ... Weird. I can see how that would be confusing in that case. It would never even occur to me to think of it like that, but I've used Math.min and Math.max reasonably often.
🌐
Dmitri Pavlutin
dmitripavlutin.com › javascript-math-max-infinity
Why Math.max() Without Arguments Returns -Infinity
Making a parallel between max and addition, -Infinity for max is the same as 0 for addition. The same behavior happens with Math.min() — it returns Infinity when called without arguments.
🌐
DEV Community
dev.to › dance2die › math-min-returns-infinity-1bi6 › comments
[Discussion] Math.min returns Infinity? — DEV Community
June 23, 2019 - Preface I was playing around with "Math.min/max" and the result of empty calls looked like it was backwards. Question Shou... ... I agree. Given no arguments, the maximum number should be -Infinity, since that is the max of nothing.
Find elsewhere
🌐
javascriptroom
javascriptroom.com › blog › why-does-math-min-return-positive-infinity-while-math-max-returns-negative-infinity
Why Does JavaScript's Math.min() Return Infinity and Math.max() Return -Infinity When Called With No Parameters?
This aligns with the identity element logic: combining an empty set of numbers with min or max operations returns their respective identity elements (Infinity for min, -Infinity for max).
🌐
W3Schools
w3schools.com › jsref › jsref_min.asp
JavaScript Math min() Method
The Math.min() method returns the number with the lowest value.
🌐
MDN Web Docs
developer-mozilla-org.translate.goog › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › min
Math.min() - JavaScript | MDN
The Math.min() static method returns the smallest of the numbers given as input parameters, or Infinity if there are no parameters.
🌐
GitConnected
levelup.gitconnected.com › why-math-max-is-less-than-math-min-in-javascript-7aaf2c39ee36
Why Math.max() is Less Than Math.min() in JavaScript | by Dr. Derek Austin 🥳 | Level Up Coding
January 5, 2023 - Did you know that Math.max() with no arguments returns a value that is smaller than Math.min() with no arguments in JavaScript? Why is that? Let’s check what the functions return: That’s weird — Infinity is actually the biggest number in JavaScript, along with the values Number.MAX_VALUE and Number.MAX_SAFE_INTEGER.
🌐
Reddit
reddit.com › r/learnjavascript › interesting........any explanations why math.max return negative infinity?
r/learnjavascript on Reddit: Interesting........Any explanations why Math.max return negative infinity?
July 18, 2021 - The way Math.maxworks is that it starts at Infinity and works its way down until either it hits -Infinity or one of the values passed in. And of course Math.min works in reverse, starting at -Infinity and working up to Infinity.
🌐
GitHub
github.com › jashkenas › underscore › issues › 2646
_.max and _.min return Infinity on empty arrays · Issue #2646 · jashkenas/underscore
January 18, 2017 - Math.max() returns Infinity too so I appreciate that's probably what's causing this, but it seems like a dumb behaviour.
Author   ghiculescu
🌐
Medium
medium.com › @vdsnini › math-max-is-less-than-math-min-in-javascript-80f678c80e3a
Math.max() is Less than Math.min() in JavaScript | by Beenish Khan | Medium
July 4, 2024 - The Edge Case: No Arguments Provided When Math.max() and Math.min() are called without any arguments, they return -Infinity and Infinity, respectively. This behaviour is due to the mathematical properties of infinity: // Math.max() with no ...
🌐
Scaler
scaler.com › home › topics › math min() javascript function
Math min() JavaScript Function - Scaler Topics
May 4, 2023 - In case no value is passed to the function it will return positive infinity as it compares the first value with positive infinity and in absence of a first value, the first comparison will be itself.
🌐
DEV Community
dev.to › salyadav › little-shenanigans-of-javascript-2-2dkb
Little Shenanigans of JavaScript - Max is Negative Infinity? WHAT! - DEV Community
July 18, 2020 - Then, when you compare your first finite number with this, it will always pick that number as minimum. Imagine if Math.min() was -Infinity. No matter what you compare this with, it will always pick your -Infinity.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › math_min_int.htm
Java - Math min(int x, int y) Method
The Java Math min(int a, int b) returns the smaller of two int values. That is, the result is the value closer to negative infinity. If the arguments have the same value, the result is that same value.