Preface
I was playing around with "Math.min/max" and the result of empty calls looked like it was backwards.
Question
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.
But...
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?