DEV Community

Discussion on: What is the oddest JavaScript behavior?

Collapse
 
titi profile image
Thibault ROHMER
Math.min() < Math.max() // false

eheh

This is why: ecma-international.org/ecma-262/5....

max: If no arguments are given, the result is −∞.
min: If no arguments are given, the result is +∞.

Math.min() === Infinity // true
Math.max() === -Infinity // true

Thus

Math.min() < Math.max()
// is the same as
Infinity < -Infinity
// which is false obviously

Now, could you say why

Math.min < Math.max // false
Collapse
 
jamesmcguigan profile image
James McGuigan

I think this was designed for the more common usecase of search algorithms looking for new minimum or maximum values.

You will always be greater than the max of an empty list, and always less than the min of an empty list, without needing to check for the edgecase of an empty list.

seen_values = [];
[1,-2,3,-5,4].foreach((value) => {
  if( value > Math.max(seen_values) ) { 
    onNewMaxValue(value); 
  }
  if( value < Math.min(seen_values) ) { 
    onNewMinValue(value); 
  }
  seen_values.push( value )
})