In Javascript, Math is a built-in object that has properties and methods for mathematical constants and functions.
max() and min() are static methods of Math, you always use it as Math.max(), Math.min()

The number arguments that min and max methods can take are zero or more

1
2
Math.max([value1[, value2[, ...]]])
Math.min([value1[, value2[, ...]]])

All is well in the JS land if you give 1 or more parameters for both min and max methods. The tricky part is if you do not provide any parameter to these methods. Now why wouldn’t you provide any parameters to max/min methods, that’s altogether a different discussion. However, because the api allows the programmer to provide zero or more parameters, the result of such operation is well documented.

Math.max() gives -Infinity.
Math.min() gives Infinity.

This makes perfect sense, because if you are just providing one parameter to math and min methods the result is always the same as the input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Math.max()
-Infinity
 
Math.min()
Infinity
 
Math.max() > Math.min()
false
 
Math.max(1,3,2)
3
 
Math.min(1,3,2)
1
 
Math.max(2)
2
 
Math.min(2)
2
 
Math.max(3) > Math.min(1)
true
 
Math.max(1) > Math.min(3)
false
“For a given programming language the language specification will be your best resource to clear out the ambiguity in your understanding.”
-Rushi
  1. Madhamanchi Anjaneyulu

    why it is returning -infinity and +infinity ? Any algorithm level explanation is really helpful!

    Is it mean Math.max() lower boundary is fixed i.e, -Infinity not upper boundary? If we pass any value to the max() method it’s not lower than lower boundary value i.e, -Infinity.
    Is it mean Math.min() upper boundary is fixed i.e, +infinity not lower boundary? If we pass any value to the min() method it’s not upper than upper boundary value i.e, +Infinity.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>