this post was submitted on 25 Jan 2024
647 points (97.8% liked)

Programmer Humor

19809 readers
250 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 16 points 10 months ago (1 children)

JS's == has some gotchas and you almost never want to use it. So === is what == should have been.

All examples are true:

"1" == true
[1, 2] == "1,2" 
" " == false
null == undefined 

It isn't that insane. But some invariants that you may expect don't hold.

"" == 0
"0" == 0
"" != "0" 
[–] Feathercrown 5 points 10 months ago (1 children)

One neat feature is you can compare to both null and undefined at the same time, without other falsey values giving false positives. Although that's not necessary as often now that we have nullish coalescing and optional chaining.

[–] [email protected] 2 points 10 months ago (1 children)

I just tested and Terser will convert v === null || v === undefined to null==v. Personally I would prefer to read the code that explicitly shows that it is checking for both and let my minifier/optimizer worry about generating compact code.

[–] SzethFriendOfNimi 0 points 10 months ago (1 children)

Try changing to const === variable. That’s most likely what’s it doing to minimize the risk of accidental assignment.

[–] [email protected] 3 points 10 months ago (1 children)

Wut? This is an automated optimizer. It is not worried about accidental assignment.

[–] SzethFriendOfNimi 1 points 10 months ago (1 children)

I agree it shouldn’t. But I’ve seen linters that automatically change it since they seem to be forcing practical conventions sometimes.

[–] [email protected] 3 points 10 months ago (1 children)

Linters and minifers are completely different tools.

[–] SzethFriendOfNimi 3 points 10 months ago

Good point. That’s what I get for shooting from the hip.

Thanks!