Breakdown
Let's start by breaking down the expression. It essentially has three parts, two static methods, and a greater than operator between them.
I don't want to read through the article, give me TLDR;
Greater than operator >
The greater than (>
) operator returns true
if the left operand is greater than the right operand and false
otherwise. This is very straightforward.
Now, let's look at the two functions.
Math.min()
and Math.max()
Math.min()
and Math.max()
are static methods of the Math
class that takes in a set of numbers as input arguments and returns the smallest or the largest number from the set respectively.
Let's look at some examples.
/* Math.min() */
console.log(Math.min(5, 2, 9, 1, 7)); // Output: 1
console.log(Math.min(-3, -10, -7, -8)); // Output: -10
console.log(Math.min(-5, -2, 8, 10, 6)); // Output: -5
/* Math.max() */
console.log(Math.max(5, 2, 9, 1, 7)); // Output: 9
console.log(Math.max(-3, -10, -7, -8)); // Output: -3
console.log(Math.max(-5, -2, 8, 10, 6)); // Output: 10
What If We Pass Only One Argument
In the examples, we saw that min()
and max()
methods are basically comparing the values present in the set and returning the smallest or the largest value respectively, but what if we use a set with only one number and pass it as an argument.
/* Math.min() */
console.log(Math.min(7)); // Output: 7
/* Math.max() */
console.log(Math.max(7)); // Output: 7
Both method returns the input number, that is because both methods have an implicit argument that they use in the comparison set, and when only one number is passed in that set, it compares that number against the implicit argument.
What Are These Implicit Arguments
min()
and max()
both use different implicit arguments.
Math.min()
As min()
returns the smallest number from the input set, it uses the largest number as the implicit argument. That is the Infinity
as no number can be larger than the infinity, a comparison against it will always return the input number.
Math.max()
Similarly max()
returns the largest number from the input set, it uses the smallest number as the implicit argument. That is the -Infinity
as no number can be smaller than the negative infinity, a comparison against it will always return the input number.
The Expression Math.min() > Math.max()
We saw that if we pass only one number in the input set to the min()
and max()
methods it returns that number and why it does that.
Now, what if we don't pass any input arguments?
/* Math.min() */
console.log(Math.min()); // Output: Infinity
/* Math.max() */
console.log(Math.max()); // Output: -Infinity
Both methods returns their implicit arguments that is, Infinity
and -Infinity
respectively.
Now, we know the return values of Math.min()
and Math.max()
, and we also know that the Greater Than >
operator compares the values and not the function expression.
So what if we compare the return values of Math.min()
and Math.max()
?
console.log(Infinity > -Infinity) // Output: true
Therefore.
console.log(Math.min() > Math.max()); // Output: true
Hence, Math.min() > Math.max() in JavaScript Makes Complete Sense as It Is.
It is not only true for the Math.min() > Math.max()
expression, but any expression where the return value of min()
is greater than the value of max()
. e.g.
console.log(Math.min(5, 2, 9, 1, 7) > Math.max(-3, -10, -7, -8)); // Output: true
console.log(Math.min(-5, -2, 8, 10, 6) > Math.max(-9, -10, -7, -8)); // Output: true
console.log(Math.min(-5, -2, 8, 10, 6) > Math.max(-3, -10, -7, -8)); // Output: false
Conclusion
Sometimes we can fall victim to logical fallacies and Math.min() > Math.max()
is a really nice example of that, it doesn't feel weird when we have sets of numbers where the return value of min()
is greater than the return value of max()
as in the example above.
However, when we see an empty set of arguments, we start comparing the meaning of min()
(minimum) and max()
(maximum) instead of comparing their return values and that leads to the logical fallacy that Math.min()
shouldn't be greater than Math.max()
and hence it should be a quirk of Javascript, but as you saw in this article it makes complete sense why it is true
and it would be absurd if it wasn't true
.
I hope you enjoyed this article and learned something new or refreshed your knowledge and I hope it makes you like Javascript a little bit more :).
I like to connect and talk with people about tech in general and the events surrounding it. You can connect with me on Twitter/X and LinkedIn. You can also follow me on dev.to.
Top comments (0)