javascriptroom blog

Why Does JavaScript's Math.min() Return Infinity and Math.max() Return -Infinity When Called With No Parameters?

JavaScript’s Math object provides a suite of utility functions for mathematical operations, and two of the most commonly used are Math.min() and Math.max(). As their names suggest, Math.min() returns the smallest number from a list of arguments, while Math.max() returns the largest. But if you call either function with no arguments at all, you might be surprised by the results:

Math.min(); // Returns: Infinity
Math.max(); // Returns: -Infinity

At first glance, this seems counterintuitive. Why would "minimum" return infinity and "maximum" return negative infinity when there are no numbers to compare? Is this a bug, or is there a deeper logic at play?

In this blog, we’ll unpack the mathematical and practical reasons behind this behavior, explore its implications for your code, and see why it’s actually a deliberate and useful design choice.

2025-11

Table of Contents#

  1. Understanding Math.min() and Math.max()
  2. The Case of No Parameters: What Happens?
  3. Mathematical Foundations: Identity Elements
  4. Practical Implications in Code
  5. Edge Cases and Examples
  6. Why Not Throw an Error?
  7. Conclusion
  8. References

Understanding Math.min() and Math.max()#

Before diving into the "no arguments" case, let’s recap how Math.min() and Math.max() work under normal circumstances.

  • Math.min(...values): Accepts zero or more numeric arguments and returns the smallest value. If any argument is not a number (and cannot be converted to one), it returns NaN.
  • Math.max(...values): Accepts zero or more numeric arguments and returns the largest value. Similarly, non-numeric arguments result in NaN.

Examples with arguments:

Math.min(2, 5, 1); // Returns: 1 (smallest of 2, 5, 1)
Math.max(3, -1, 4); // Returns: 4 (largest of 3, -1, 4)
Math.min(10, Infinity, 5); // Returns: 5 (since 5 < 10 < Infinity)
Math.max(-5, -Infinity, 0); // Returns: 0 (since -Infinity < -5 < 0)

These functions are also frequently used with arrays, via the spread operator (...):

const numbers = [3, 7, 2];
Math.min(...numbers); // Returns: 2
Math.max(...numbers); // Returns: 7

The Case of No Parameters: What Happens?#

Now, let’s consider the scenario where we call Math.min() or Math.max() with no arguments (or equivalently, with an empty array using the spread operator):

Math.min(); // Infinity
Math.max(); // -Infinity
 
// Equivalent with empty arrays:
Math.min(...[]); // Infinity
Math.max(...[]); // -Infinity

This behavior is specified in the ECMAScript standard, but why is it defined this way? To answer that, we need to delve into mathematics—specifically, the concept of "identity elements" and the properties of min/max operations on empty sets.

Mathematical Foundations: Identity Elements#

To understand the "no arguments" case, we first need to recall what an identity element is in mathematics. An identity element (or "neutral element") for an operation is a value that, when combined with another value using that operation, leaves the other value unchanged.

For example:

  • In addition, the identity element is 0, because x + 0 = x for any number x.
  • In multiplication, the identity element is 1, because x * 1 = x for any number x.

For min and max operations, we need an identity element such that:

  • min(identity, x) = x for any number x (identity for min).
  • max(identity, x) = x for any number x (identity for max).

Identity Element for Math.min(): Infinity#

What value satisfies min(identity, x) = x for any x? Let’s test candidates:

  • If we pick 0: min(0, 5) = 0 (not 5), so 0 fails.
  • If we pick 100: min(100, 5) = 5 (good), but min(100, 200) = 100 (not 200), so 100 fails.
  • If we pick Infinity: min(Infinity, x) = x for any x. Because any real number is smaller than Infinity, so the minimum of Infinity and x is always x.

Thus, the identity element for min is Infinity.

Identity Element for Math.max(): -Infinity#

Similarly, what value satisfies max(identity, x) = x for any x?

  • If we pick 0: max(0, -5) = 0 (not -5), so 0 fails.
  • If we pick -100: max(-100, -5) = -5 (good), but max(-100, -200) = -100 (not -200), so -100 fails.
  • If we pick -Infinity: max(-Infinity, x) = x for any x. Because any real number is larger than -Infinity, so the maximum of -Infinity and x is always x.

Thus, the identity element for max is -Infinity.

The Empty Set: Why Identity Elements Matter#

When Math.min() or Math.max() is called with no arguments, it’s equivalent to computing the min or max of an empty set of numbers. In mathematics:

  • The minimum of an empty set is defined as Infinity (since there are no numbers to compare, the "smallest" value is unbounded above, hence Infinity).
  • The maximum of an empty set is defined as -Infinity (similarly, the "largest" value is unbounded below, hence -Infinity).

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).

Practical Implications in Code#

This behavior isn’t just a mathematical curiosity—it’s intentionally designed to make Math.min() and Math.max() work seamlessly with common programming patterns.

Example 1: Calculating Min/Max of a Dynamic Array#

Suppose you need to find the minimum value in an array that might be empty (e.g., user-generated data). A common pattern is to initialize a variable with the identity element and iterate through the array:

function findMin(numbers) {
  let minVal = Infinity; // Start with identity for min
  for (const num of numbers) {
    minVal = Math.min(minVal, num); // Update with current number
  }
  return minVal;
}
 
findMin([3, 1, 4]); // 1 (correct)
findMin([]); // Infinity (matches Math.min())

If the array is empty, minVal remains Infinity—exactly what Math.min() returns when called with no arguments. The same logic applies to max with -Infinity:

function findMax(numbers) {
  let maxVal = -Infinity; // Start with identity for max
  for (const num of numbers) {
    maxVal = Math.max(maxVal, num);
  }
  return maxVal;
}
 
findMax([-2, 5, 0]); // 5 (correct)
findMax([]); // -Infinity (matches Math.max())

Example 2: Consistent Behavior with Spread Operator#

When using the spread operator with an empty array, Math.min(...[]) and Math.max(...[]) behave exactly like the no-argument case. This consistency avoids unexpected edge cases in code:

const emptyArray = [];
Math.min(...emptyArray); // Infinity (same as Math.min())
Math.max(...emptyArray); // -Infinity (same as Math.max())

Edge Cases and Examples#

Let’s explore a few more scenarios to solidify understanding:

Non-Numeric Arguments#

If you pass non-numeric arguments (that can’t be converted to numbers), both functions return NaN—even if other arguments are valid:

Math.min(2, "not a number"); // NaN
Math.max(5, undefined); // NaN (undefined converts to NaN)

But with only non-numeric arguments, the result still follows the identity logic if all convert to NaN:

Math.min(NaN); // NaN (since NaN is not a number)
Math.max(NaN, "oops"); // NaN

Mixing Identity Elements with Valid Numbers#

As expected, combining the identity element with actual numbers returns the number itself:

Math.min(Infinity, 42); // 42 (since 42 < Infinity)
Math.max(-Infinity, -7); // -7 (since -7 > -Infinity)

Why Not Throw an Error?#

You might wonder: Why not throw an error when Math.min() or Math.max() is called with no arguments? After all, an empty set has no "minimum" or "maximum" in a colloquial sense.

The answer lies in utility and consistency. Throwing an error would force developers to handle empty inputs explicitly (e.g., checking if an array is empty before calling Math.min(...array)). Instead, returning the identity elements:

  • Aligns with mathematical definitions of min/max for empty sets.
  • Enables seamless integration with common patterns (e.g., initializing accumulators with Infinity/-Infinity).
  • Avoids breaking code that relies on dynamic input (e.g., arrays that may or may not be empty).

Conclusion#

The behavior of Math.min() returning Infinity and Math.max() returning -Infinity with no arguments is not a quirk—it’s a deliberate design choice rooted in mathematics. By returning their respective identity elements (Infinity for min, -Infinity for max), these functions align with the mathematical definitions of min/max for empty sets and enable practical, error-resistant code patterns.

Next time you encounter an empty array and need to compute its min or max, remember: Infinity and -Infinity aren’t bugs—they’re features!

References#