Did you know that Math.max() < Math.min() in JavaScript?

It's a fun fact about the language that is periodically brought up, but do you know why this actually happens?

Let's take a quick look at Math.max and Math.min to understand the issue.

A thread.
1. Is this a quirk or a bug?

No, it is neither a quirk nor a bug.

There are two things you need to realize first:

1. Math.max and Math.min are functions
2. JavaScript is dynamic

1/21
-> Math.max() and .min() are functions

Both functions are actually not meant to return any maximum or minimum displayable value for numbers.

Both are meant to find the max or min value from:

1. Two arguments
2. An array of values

2/21
-> JavaScript is dynamic

Any function can take an arbitrary amount of parameters, although only a few might actually be named in a function's signature.

Those parameters are always type-less because JavaScript is a dynamically typed language.

3/21 function myFunction(arg1, arg2, arg3) {   // processing...
This leads to that you can't have multiple versions of a function with different parameter lengths or parameter types.

There is always only one version of a function.

If you want to support something like this, you need to implement it manually.

4/21
Thus, the implementation needs to support:

1. 0 arguments passed
2. one argument passed
3. two arguments passed
4. more than two arguments passed

And all this within one function implementation.

5/21
2. Understanding the behavior

Let's imagine you want to implement Math.max as an example.

What would you need to do to find the maximum of an arbitrary amount of values?

You need a value to compare against.

6/21
If you get two values, you would be fine to compare the two values and return the greater one.

This covers one case.

7/21 function max(valueOne, valueTwo) {   // JS is dynamic, we ne
But what if you only get one value?

The max value of one argument provided must be the value itself.

8/21 function max(valueOne, valueTwo) {   // covering the case of
Okay, but what now? You still need to cover the case that an arbitrary amount of values is provided.

As you learned previously, 'arguments' is a special variable in a function that contains all arguments that were passed to a function.

You can loop over these.

9/21
But you still need a value to compare against.

The first iteration needs to check whether the first argument is greater than something.

But what is this something?

Is it 0? No, because what about -1, then? -1 could still be greater than -2 and -3...

10/21
Well, the smallest value possible is -Infinity. Every other value anyone could supply to your function would be greater than -Infinity.

This is the value you need to compare against.

11/21 function max(valueOne, valueTwo) {   const length = argument
This implementation does not cover some special cases.

The function also needs to handle NaN, as well as +0 and -0, but this would probably be too much for this thread.

The implementation at hand is still pretty accurate and shows why Math.max() returns -Infinity.

12/21
-Infinity is simply the smallest value possible to compare all other values against.

+Infinity is the greatest value possible and is the value the minimum is initialized with for Math.min.

13/21
Math.max() returns -Infinity.

Math.min() returns +Infinity.

-Infinity < +Infinity

Thus Math.max() < Math.min()

14/21
3. Taking a look at the ECMA spec

This is what the spec says for Math.max.

Source: 262.ecma-international.org/12.0/#sec-math…

15/21 1. Let coerced be a new empty List. 2. For each element arg
This is what the spec says for Math.min.

Source: 262.ecma-international.org/12.0/#sec-math…

16/21 1. Let coerced be a new empty List. 2. For each element arg
As you see, our own implementation above hit the spec relatively well, except for the special cases.

17/21
4. Taking a look at V8's implementations

This is V8's implementation of Math.max:

Source: github.com/v8/v8/blob/cd8…

18/21 function MathMax(arg1, arg2) {  // length == 2   var length
And this is V8's implementation of Math.min:

Source: github.com/v8/v8/blob/cd8…

19/21 function MathMin(arg1, arg2) {  // length == 2   var length
All this combined should now clear up for you why this funny behavior occurs.

It is an implementation detail that was specifically decided to be leaked to the outside.

20/21
5. Thread end

That's it for this thread.

I hope you found this useful and learned something valuable. 🙏🏼

If you enjoy content like this, leave a like, retweet the first tweet, and follow me (@oliverjumpertz) for more content like this!

21/21

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Oliver Jumpertz

Oliver Jumpertz Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @oliverjumpertz

19 Jul
"Why should we hire you?"

This is another of those questions everyone interviewing hates.

It spread from traditional jobs into the tech world, and even software developers have to deal with it.

But you can turn this into a huge win. Let's see how.

A thread. ↓
1. Why is this question asked?

Like any of those pretty cliché questions regularly asked in interviews, interviewers try to find out whether you are a good fit.

Your hard skills might have been assessed already or will be soon, but this one is about your character.

1/32
Even before the term EQ (emotional intelligence) became popular, there was more to employees than only the skills they brought with them.

People are human. They have a character. And they need to get along with other people.

2/32
Read 33 tweets
18 Jul
Did you know that calling yourself something like "Junior JavaScript Developer" on your CV and socials is one of the worst things you can do for your career?

It immediately strips away a lot of your credibility and can often even close some doors.

A thread.
1. What is wrong with this title?

It might seem perfectly fine to call yourself what you think you actually are, but it is not.

It describes yourself too specifically, and it shuts some doors for you.

1/15
This title is one of the first things a recruiter or an interviewer sees on your CV, and it already tells them a lot about you.

This is your description. It is a short, concise statement about who you are and what you do.

It should state: "This is who I really am."

2/15
Read 16 tweets
17 Jul
"Where do you see yourself in five years?"

This is still a common job interview question.

But do you hate it as much as I do and would love to stand up and simply leave immediately?

Don't.

Here is how you can turn this question into a huge win.

A thread. ↓
0. Foreword

I've interviewed countless times in the last years and had the honor to interview some amazing engineers myself.

I never used this question myself but had to witness HR ask exactly this one.

1/40
It took me a good amount of time to understand that, while being a question most interviewees hate, it's an HR trick to gather valuable information.

2/40
Read 41 tweets
17 Jul
Debugging 101

1. Start your debugger
2. Trigger an action
3. Skip the first 10 breakpoints you forgot to remove last time
4. Realize you forgot to set a new breakpoint
5. Abort
6. Set a new breakpoint
7. Retrigger the action

👇🏻
8. Sigh because you need to skip the 10 first breakpoints again
9. Realize you accidentally also skipped your new breakpoint
10. Retrigger the action
11. Skip and remove each of the first 10 breakpoints you forgot to remove once again
12. Stop at the right breakpoint

👇🏻
13. Stare at your screen and think
14. Realize you should have set the breakpoint earlier
15. Abort
16. Set a new breakpoint
17. Retrigger the action
18. Stare at your screen again
19. Think and cry a little
20. Take a break

👇🏻
Read 4 tweets
16 Jul
💛 HTML tip 💛

You can force a user's browser to open a new tab when clicking on a link by adding target="_blank" to it.

But you should always add rel="noreferrer" to it!

The reason and an explanation in a short thread. ↓ <a href="https://anydomain.com" target="_blan
When a new tab opens, the page loaded gets access to the window.opener attribute. If the site has malicious code in it, it can use the opener property to redirect your page to another malicious site that might harm the user.

1/7
Adding rel="noreferrer" prevents that the window.opener property is accessible for the site linked to and additionally skips sending a Referer HTTP header to that page with your website's URL.

2/7
Read 8 tweets
15 Jul
"Are there any questions left we can answer?"

"No, not right now, thank you."

Have you ever encountered this situation in an interview and responded like above?

I'm sorry, but you've given away an awesome opportunity to make a lasting impression.

A thread.
0. Foreword

I've been on both sides of the interview table for a few years now.

I've interviewed for positions, and I've interviewed quite a few awesome engineers.

I can only give you my perspective and opinion. It does not necessarily need to be fitting each situation.

1/32
1. Why this question is an opportunity

Although there is a shortage of software developers right and left, many companies still want to make sure they hire the right people.

Even the best engineers may not be the best fit for a company and its culture.

2/32
Read 34 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(