Weird Behaviour in JS Explained: Math.min() > Math.max()

Search for a command to run...

No comments yet. Be the first to comment.
What is JavaScript? Discover how this quirky yet powerful language runs in browsers, what makes it tick, and how you can start adding it to your sites

Problem If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters w...

Problem 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000? Problem Description The problem is pretty self-explanatory. 215 is 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26....

Problem Starting in the top left corner of a 2*2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner. How many such routes are there through a 20*20 grid? Problem Description We are given a...

Problem The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → ...

Javascript is indeed a great language. But at times it is tricky, funny, behaves weirdly and, what-not.
One Such weird fact is,

But how?
Let me explain.
First let us understand what Math.min() and Math.max() functions are.
Math.min(): When Zero or more arguments are passed, it returns the smallest of the values orNaNif any parameter isn't a number or can't be converted into one.
Examples,

Math.max(): When Zero or more arguments are passed, it returns the largest of the values orNaNif any parameter isn't a number or can't be converted into one.
Examples,

So What happens when there is zero arguments to theMath.min() and Math.max() functions.

The Math.max() function returns -Infinity while the Math.min() function returns +Infinity
Why this weird behavior?
To know about it we need to look into the internal implementation of its algorithm
According to TC39 - Where Javascript Standard (ECMAScript) is defined, Math.min() and Math.max() the smallest or largest respectively is determined using IsLessThan algorithm is used except when +0 and -0 comparison is involved where +0 is the largest.
The Pseudocode or the steps as defined by TC39, in a pure Javascript function form:
function minimum(...args) {
let temp = [];
let smallest = Infinity;
for(let i=0; i<args.length; i++) {
temp.push(Number(args[i])); //
}
for (let i = 0; i < temp.length; i++) {
if(temp[i].isNaN()) {
return temp[i];
}
if(temp[i] < smallest) {
smallest = temp[i];
}
}
return smallest;
}
function maximum(...args) {
let temp = [];
let largest = -Infinity;
for(let i=0; i<args.length; i++) {
temp.push(Number(args[i]));
}
for (let i = 0; i < temp.length; i++) {
if(temp[i].isNaN()) {
return temp[i];
}
if(largest < temp[i] {
largest = temp[i];
}
}
return largest;
}
console.log(minimum(99,'2',3, -54)); // -54
console.log(maximum(99,'2',3, -54)); // 99
console.log(minimum(true, '45', -22, 0, 1999)); // -22
console.log(maximum(true, '45', -22, 0, 1999)); // 1999
console.log(minimum(true, false)); // 0
console.log(maximum(true, false)); // 1
console.log(minimum()); // Infinity
console.log(maximum()); // -Infinity
Let us walk through the above code.
...argstemp be a temporary array, which is used for type conversion.smallest, the initial comparant, be Infinity as IsLessThan algorithm is used and almost every other value is smallerargs are checked for the Number type, if not, they will be converted to Number and appended to the temp array. The below table gives an overall idea of this conversion.| Type | Return Result |
| Undefined | Return NaN. |
| Null | Return +0. |
| Boolean | If true, return 1. If false, return +0. |
| Number | Return as such (no conversion). |
| String | If there is a Number within String, returns the number else returns NaN. |
| Symbol | Throw a TypeError exception. |
| BigInt | Throw a TypeError exception. |
NaN, then the function returns NaN. Else it compares the whole array with the smallest and returns the smallest number.All steps are repeated for Math.max(), except the initial Comparant is set to -Infinity and almost every other value is larger.
So the Math.min() and Math.max() with zero values passed, returns Infinity & -Infinity respectively attributing to the weird behaviour we had seen before, Math.Min() is greater than Math.min().
Also, check the V8 implementation of Math.min() & Math.max() can be found at this github repo.
If I have missed any points, please let me know in the comments.