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

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

One Such weird fact is,
![Math.min() _ Math.max().png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626440746530/_n5_7vENQ.png)

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 or `NaN` if any parameter isn't a number or can't be converted into one.

Examples,

![Math.min() Examples (1).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626440775939/zYluNMyyq.png)

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

Examples,
![Math.max() Examples.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626440792633/IZ9tZ3Whi.png)

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

![Infinity Value.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1626440822922/vdT3I6Vz2J.png)

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

## Internal Implementation of the algorithm

According to TC39 - Where Javascript Standard (ECMAScript) is defined, [`Math.min()`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-math.min)  and [`Math.max()`](https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-math.max) the smallest or largest respectively is determined using [`IsLessThan` algorithm](https://tc39.es/ecma262/multipage/abstract-operations.html#sec-islessthan) 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:

```jsx
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. 

### Math.min() function - minimum()

- When zero or more arguments which form the rest parameter `...args`
- Let `temp` be a temporary array, which is used for type conversion.
- Let `smallest`, the initial comparant, be `Infinity` as IsLessThan algorithm is used and almost every other value is smaller
- Now all the values in the `args` 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. |

- Now if there is any `NaN`, then the function returns `NaN`. Else it compares the whole array with the `smallest` and returns the smallest number.

### Math.max() function - maximum()
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](https://github.com/v8/v8/blob/cd81dd6d740ff82a1abbc68615e8769bd467f91e/src/js/math.js#L77-L102) repo.

If I have missed any points, please let me know in the comments.
