r/learnjavascript • • Nov 01 '22

Yea...

Post image
232 Upvotes

25 comments sorted by

85

u/delventhalz Nov 01 '22 edited Nov 01 '22

FWIW, == basically works like this:

  1. Don't use ==

  2. It's silly

  3. You don't need to know how it works

But okay, if you really want to know, you can pretty much always work out how two values will loosely compare, by following these steps:

  1. If two values are the same type, check if they are strictly equal

  2. If one value is null or undefined, return true if the other value is also null or undefined

  3. If one value is an object, convert it to a primitive (almost always a string), and then go back to step 1

  4. If one value is a string or a boolean, convert it to a number, then go back to step 1

So in all of these examples there is an array getting stringified. For an array, the default toString behavior is the same as if you used join. Elements get stringified and joined with a ,, unless they are null or undefined, then they get converted into an empty string.

[0, null, undefined, false].join();   // 0,,,false
String([0, null, undefined, false]);  // 0,,,false

If you look at the examples in the image, they all contain one element, so no commas. And that one element is almost always an empty string, null, undefined, or another array which contains an empty string, null, or undefined. In other words, they almost all will stringify to an empty string.

String([]);           // ""
String(['']);         // ""
String([undefined]);  // ""
String([[[ null ]]]); // ""

The one exception are the ones which contain 0, which will stringify to "0".

String([0]);  // "0"

So half of these comparisons just convert to "" == "", which is true. The rest also go through the number conversion in Step 4. And what happens when you convert an empty string or "0" to a number?

Number("");   // 0
Number("0");  // 0

The one that might still confuse is the one false example:

[0] == "";  // false

But remember the order of operations here. We have an object and a primitive, we convert the object first.

"0" == "";  // false

Now, if we kept converting these strings to numbers, they would both become 0, but that is not how this work is it? As soon as you have two values of the same type you are done, no more conversions. You just check if they are strictly equal, and those are two different strings aren't they?

Anyway, Like I said up front, this is all very silly. Don't use == in JS. It was a mistake. But if you like pulling apart weird esoteric code, there you go.

4

u/Leaping_Turtle Nov 01 '22

Can explain why that single thing is false?

6

u/delventhalz Nov 01 '22

Because "0" does not equal "".

Reread the last third or so of my previous post. The stuff following:

The one that might still confuse is the one false example:

I don't think I can explain it much better than I already did, but If you still have specific follow-up questions, I would be happy to answer them.

8

u/Leaping_Turtle Nov 01 '22

I don't know how i missed that in the original comment. My bad!

2

u/mdw Nov 02 '22

Why is == in the language if it's not to be used?

3

u/delventhalz Nov 02 '22

JavaScript is 100% backwards compatible. The value null still has a typeof "object" despite that being widely acknowledged as just a bug. When you want to replace bad syntax in a new version of JS, you don’t remove the old thing, you just add something new.

So == was supplanted by ===, var was supplanted by let/const, old-school for loops were (mostly) supplanted by for…of, etc. All of this old syntax still exists, which can sometimes cause headaches, particularly for new learners, but it beats the alternative. A new version of JS that broke old code would be a nightmare for many reasons, and ultimately the browser vendors simply would not implement it.

1

u/mdw Nov 02 '22

So I am actually right when I rant that JavaScript is a poorly designed language?

I've been programming in JS for like two decades or so (not in professional capacity though) and this is the first time I hear that == shouldn't be used.

2

u/Barandis Nov 02 '22

Mostly history. Before JavaScript 1.3, and maybe before it was clear just how bad it was, double equals was the only choice for comparison.

JS has never wanted to break backwards compatibility because millions of websites would also break, so it was left in and we’re left with something in the language that you really shouldn’t use.

26

u/ProposalUnhappy9890 Nov 01 '22

At my company, the pre-commit linter hook will fail if you try to write == instead of ===. I suggest you do the same.

6

u/metallaholic Nov 01 '22

--no-verify

7

u/waitersweep Nov 01 '22

And fail in CI instead

2

u/linusl Nov 01 '22

and leave the rest of your team with your errors?

no.

I would also notice this quickly in code review and tell you to fix your commit hook.

1

u/metallaholic Nov 01 '22

Who said I did it. That’s something I see others do. And low and behold when I do a pull origin develop the linter is screaming.

3

u/linusl Nov 01 '22

your comment is ambiguous but my reply is toward anyone else as much as you. this a subreddit for learning and I don’t want anyone to get ideas or learn wrong things from joke or sarcasm or lack of clarity.

sounds like you understand that you have problem in your team so that’s a good start. hopefully you will find a way to have more strict rules to avoid issues like this.

6

u/grograman Nov 01 '22

This is pretty neat, but also yeah, that's why you don't use ==.

5

u/DbrDbr Nov 01 '22

Use strict and ESLint.

9

u/shgysk8zer0 Nov 01 '22

Gee, I've never seen a meme about implicit type coercion and the use of == before. How brilliant and original and informative and insightful.

1

u/lovin-dem-sandwiches Nov 01 '22

hahah, the amount of snark in this comment is gold

5

u/shgysk8zer0 Nov 01 '22

What do you mean? This is the very first time I've ever seen a meme like this, and it taught me so much about JavaScript. I definitely haven't seen this same joke without the Star Wars reference 6 times just in the last hour, and I imagine it wouldn't get old even if I saw it multiple times a day for 5 years. And I just love how it taught me why all of that makes sense when using == and that === exists to basically eliminate these situations that are unintuitive to a beginner and require more thought than they should. After seeing this meme, I feel like I'm an expert in JavaScript... But it's a stupid language that doesn't make any sense, so I'll switch to brainfuck or something.

I seriously hate type coercion memes... And floating point memes... And NaN === NaN memes. They all stopped being funny after the 100th time I saw them, and that was probably only my second day on Reddit.

1

u/Chalfari Nov 02 '22

Is there no use case where your front end needs the flexibility of type coersion using == if you don't know 100% what to expect from the backend? I mean there is. My question is is it OK to use in that case or better to check multiple values with strict comparison using OR?

1

u/hanoian Nov 02 '22

Multiple. And you can be fancy and do something like ["value1", "value2].includes("valuex") to be neater.