DEV Community

Cover image for Math.min returns Infinity?
Sung M. Kim
Sung M. Kim

Posted on

Math.min returns Infinity?


I was playing around with "Math.min/max" and the result of empty calls looked like it was backwards.

empty math min max results


Shouldn't Math.min return -Infinity while Math.max, Infinity?

MDN Documentations

MDN documentation on Math.max shows that

If no arguments are given, the result is -Infinity.

And Math.min documentation shows

If no arguments are given, the result is Infinity.

But why? 🤔

After some thinking, it made sense.

Suppose that you are passing one value to Math.min(3). The minimum should 3 as it's the sole value pass to the function. 3 should be lower than whatever the minimum JavaScript has to compare.

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.

Same thing for Math.max. If you call Math.max(3), 3 is bigger than -Infinity thus, 3 is returned.


I am not exactly sure if my thought process is correct or not but at least it helps to understand what default values are returned when no argument is passed to Math.min/max functions.

Would anyone let me know if I understood the reason behind the return values?

Top comments (22)

minimumviableperson profile image
Nicolas Marcora

I agree. Given no arguments, the maximum number should be -Infinity, since that is the max of nothing.

Your example is good, but I would push it even further than 3 to make a case.

Math.max(-100000000) should still return -100000000 as the max, meaning that, no matter how small, or negative, a number is, it should still be allowed the possibility of it being the highest number. Comparing it against -Infinity allows for that.

dance2die profile image
Sung M. Kim

Thanks Nicolas.

That makes total sense with really low negative number as no matter how small a number is, it is bigger than -Infinity

gizzzzmo profile image

You can also think of it algebraically, in that all numbers form a monoid under max and min.
The operation is associative, and there exists an identity, namely -infinity for max and infinity for min.
And if you define max/min with arbitrarily many arguments inductively, then the empty operation has to return the identity.

wwbrannon profile image
William Brannon

It's very not intuitive but I think they're following the usual mathematical convention. The max of no elements is odd to think about, but the supremum of the empty set is just its least upper bound. Because everything is an upper (and lower) bound of nothing, the least such value is -Infinity. Similarly the greatest lower bound, or infimum, or min, of the empty set is +Infinity.

See also this StackExchange discussion.

dance2die profile image
Sung M. Kim

Thank you, William.

As I was reading John's message, I ran into a concept called, monoid.
It seems like monoid stems from the "mathemtical convention" as you pointed out.

mjvmroz profile image
Michael Mroz

monoid is the first of many steps down the road that leads to functional programming. Join us.

Thread Thread
dance2die profile image
Sung M. Kim


kennethlum profile image
Kenneth Lum

I think one reason is

min(a, b, c) = min([], min(a, b, c)) = min(a, min(b, c)) = min(a, b, min(c)) = min(a, b, c, min([]))

that is, you call pull an element out and do a min and the final result is the same. You can pull the nothing out, or you can pull all out. The final form will make min([]) to be Infinity, or else say if it is -Infinity, then the final value would become -Infinity. It is like, with the absence of any value, I don't know what the minimum is and I assume it is still very big.

rferromoreno profile image
Ricardo Ferro Moreno

Most people think that Math.max() stands for MAXINT. In some languages, MAXINT is the maximum representable integer value.

In JavaScript, you can check your MAXINT using Number.MAX_SAFE_INTEGER.

Actually, Math.max() is a function that returns the max value of a given list of values.

Now, try to think about how would you implement this function. Our own "Math.max" function. It would be something like this:

Math.prototype.max = function () {
  var max = -Infinity;
  for (var i=0; i<arguments.length; i++) {
    if (arguments[i]>max) {
      max = arguments[i];
  return max;

Since no arguments are given in a call to Math.max(), then we never iterate the for-loop, and then, -Infinity is returned.

Analogously, the same happens to Math.min().

If you want to read the real Math.max specification, please check the ECMAScript standard:

dance2die profile image
Sung M. Kim

Thank you, Ricardo for the code as it makes it easier to grasp the concept
and for the link to the ECMAScript Language Specification.

It's funny that all these years using JavaScript, I've never checked out the spec 🙃

totiiimon profile image

Despite being a little bit non-intuitive... I think one problem is trying to interprete this function as something like Number.MAX_VALUE In order to compare with something to find the max value, you will always have one number to start on, and to make it to be the max or the lowest always, makes total sense to have the "current" max or min value being -Infinity and Infinity respectively.

dance2die profile image
Sung M. Kim • Edited

You are spot on with my misthinking that Math.max() === Number.MAX_VALUE 😂

umutcnkus profile image
Umutcan Kuş

Actually, even after all explanations it is still not a good design decision to me. But, your idea for reason is legit.
Another interesting thing is:

Math.min() > Math.max()

The reason is clear i think (:
Here you can see some other strange things about js, including this one.

dance2die profile image
Sung M. Kim


Thank you for leading me to the rabbit hole of JavaScript Umutcan 😉

jakebman profile image

Here's another one to blow your mind:

In Java, Math.abs(Integer.MIN_VALUE) is negative

This is per-spec:

Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.

Because |Integer.MIN_VALUE| > Integer.MAX_VALUE, there's no way to represent it as a positive value in only 32 bits. It's basically overflow. C# throws an OverflowException, for instance.
Java doesn't throw on overflow, so it handles it this way.

dance2die profile image
Sung M. Kim

You are blowing my mind 🤯💥


dance2die profile image
Sung M. Kim

Thank you, @dominela10 .

It seems like my assumption was "sorta" correct but without concrete understanding of how it was so.
Your explanation made it easy to grasp what's going on.

Lastly, learned a new term, variadic :)

lukaszwiktor profile image
Łukasz Wiktor • Edited

JavaScript as usual returns something even if it doesn't make sense...
Math.min and Math.max should throw an error if no arguments are given.

dance2die profile image
Sung M. Kim

.min/max are acting sorta like a constant as Math.PI does when a no arg is passed. Also feels like an "alias" to -/Infinity.

johnboy5358 profile image

This post has a connection with your question.

Hope it helps.

dance2die profile image
Sung M. Kim

Thank you, John. The linked post was very helpful.