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
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
5
3
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 === NaNmemes. They all stopped being funny after the 100th time I saw them, and that was probably only my second day on Reddit.
2
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.
-2
85
u/delventhalz Nov 01 '22 edited Nov 01 '22
FWIW,
==basically works like this:Don't use
==It's silly
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:
If two values are the same type, check if they are strictly equal
If one value is
nullorundefined, return true if the other value is alsonullorundefinedIf one value is an object, convert it to a primitive (almost always a string), and then go back to step 1
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 arenullorundefined, then they get converted into an empty string.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.
The one exception are the ones which contain
0, which will stringify to"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?The one that might still confuse is the one
falseexample:But remember the order of operations here. We have an object and a primitive, we convert the object first.
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.