this post was submitted on 25 Jan 2024
676 points (97.6% liked)

Programmer Humor

19589 readers
554 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 1 year ago
MODERATORS
 
top 50 comments
sorted by: hot top controversial new old
[–] Buttons@programming.dev 75 points 9 months ago* (last edited 9 months ago) (1 children)
[–] backhdlp@iusearchlinux.fyi 47 points 9 months ago* (last edited 9 months ago) (11 children)

I still don't understand the === operator

Edit: I think a more type strict ==? Pretty sure I understand the point of typescript now.

[–] SzethFriendOfNimi@lemmy.world 129 points 9 months ago* (last edited 9 months ago) (1 children)

So in JavaScript there’s the assignment

=

and the comparator is

==

Since there’s no types JS will do implicit conversion before comparison when using == in a case like this

if(false == '0'){
    //this is true
}

But with === it doesn’t. It means literally compare these

if(false === '0'){
    //this is false
}else{
    //so this will execute instead 
}

But this, however, will

var someState = false;
 if(someState === false){
    //this is true
}
[–] QuazarOmega@lemy.lol 68 points 9 months ago
> 1 == 1
true
> 1 == '1'
true
> 1 === '1'
false

(from node REPL)

Basically it's the real equals sign perfection

[–] frezik@midwest.social 46 points 9 months ago (1 children)

The short answer is that your language needs === when it fucked up the semantics of ==, but it's also too popular and you can't fix it without breaking half the web.

load more comments (1 replies)
[–] SmoothIsFast@lemmy.world 28 points 9 months ago

It's like the ==, but there's one more =

[–] kevincox@lemmy.ml 21 points 9 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@lemmy.world 5 points 9 months ago (6 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.

load more comments (6 replies)
[–] Limitless_screaming@kbin.social 18 points 9 months ago

== but for JavaScript. What you don't understand is the == of JavaScript.

[–] Mikina@programming.dev 18 points 9 months ago* (last edited 9 months ago) (2 children)

It's also important if you're checking hashes (at least, it was - if you're using correct hashing algorithm that isn't ancient, you will not have this problem).

Because if you take for example "0e462097431906509019562988736854" (which is md5("240610708"), but also applicable to most other hashing algorithms that hash to a hex string), if("0e462097431906509019562988736854" == 0) is true. So any other data that hashes to any variantion of "0e[1-9]+" will pass the check, for example:

md5("240610708") == md5("hashcatqlffzszeRcrt")

that equals to

"0e462097431906509019562988736854" == "0e242700999142460696437005736231"

which thanks to scientific notation and no strict type checking can also mean

0^462097431906509019562988736854^ == 0^242700999142460696437005736231^

which is

0 == 0 `

I did use md5 as an example because the strings are pretty short, but it's applicable to a whole lot of other hashes. And the problem is that if you use one of the strings that hash to a magic hash in a vulnerable site, it will pass the password check for any user who's password also hashes to a magic hash. There's not really a high chance of that happening, but there's still a lot of hashes that do hash to it.

[–] darcy@sh.itjust.works 10 points 9 months ago

that is terrifying

load more comments (1 replies)
[–] Bougie_Birdie@lemmy.blahaj.zone 6 points 9 months ago (1 children)

The other comments explains it in pretty good detail, but when I was learning my teacher explained it sort of like a mnemonic.

1 + 1 = 2 is read "one plus one equals two"

1 + 1 == 2 is read "one plus one is equal to two"

1 + 1 === 2 is read "one plus one is really equal to two"

And you hit the nail on the head, is that === is type explicit while == is implicit.

load more comments (1 replies)
[–] clb92@feddit.dk 4 points 9 months ago* (last edited 9 months ago)

Like == but more strict. The == operator will do type conversion, so 0 == '' will actually be true, as an example. Sometimes (honestly, most times) you may want to compare more strictly.

See this StackOverflow answer: https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons

[–] ShortFuse@lemmy.world 4 points 9 months ago* (last edited 9 months ago)

You don't need Typescript, you need an linter (eslint).

=== is your basic equality like most languages. == will implicitly cast type.

The breakdown is here: https://262.ecma-international.org/5.1/#sec-11.9.3

Modern JS says to never use == unless you're comparing against null or undefined.

load more comments (1 replies)
[–] Blackmist@feddit.uk 13 points 9 months ago

JS devs should have a font that turns == into ≈.

[–] luciole@beehaw.org 12 points 9 months ago (3 children)

I wish the assignment operator wasn’t the equal sign.

[–] QuazarOmega@lemy.lol 19 points 9 months ago (2 children)
[–] xedrak@kbin.social 13 points 9 months ago (1 children)

Ok deal, but that means we need to change the equality operator to 👉👈

[–] OpenStars@startrek.website 5 points 9 months ago

You sonnofabitch I'm in!:-P

[–] OpenStars@startrek.website 8 points 9 months ago (1 children)
x 🔫 5

the pew pew principle /s

[–] Malgas@beehaw.org 8 points 9 months ago (1 children)

Interpreter: Wait, x is 5?

This code: Always has been.

[–] OpenStars@startrek.website 4 points 9 months ago

It is now, if you know what's good for you.

load more comments (1 replies)
[–] DrunkenPirate@feddit.de 12 points 9 months ago* (last edited 9 months ago) (3 children)
load more comments (3 replies)
[–] xedrak@kbin.social 11 points 9 months ago (1 children)
load more comments (1 replies)
[–] callyral@pawb.social 10 points 9 months ago* (last edited 9 months ago) (1 children)
[–] gandalf_der_12te@feddit.de 9 points 9 months ago

Basically Java in a nutshell

[–] jenny_ball@lemmy.world 7 points 9 months ago (1 children)

it depends on what your definition of is is

load more comments (1 replies)
[–] sooper_dooper_roofer@hexbear.net 7 points 9 months ago

eight equals equals equals equals equals equals equals equals equals capital d tilde tilde

[–] GiM@lemmy.world 5 points 9 months ago (1 children)
load more comments (1 replies)
[–] mumblerfish@lemmy.world 4 points 9 months ago (3 children)

Mathematica also has an === operator. And :=.

[–] lurch@sh.itjust.works 8 points 9 months ago (1 children)

It's also very language specific, like Pascal/Delphi also have ":=" for assignments and "=" for comparison, etc

load more comments (1 replies)
load more comments (2 replies)
[–] Lucien@hexbear.net 4 points 9 months ago (3 children)
[–] _edge@discuss.tchncs.de 8 points 9 months ago* (last edited 9 months ago)
==    same (after magic)
===   same and same type (in Javascript)
====  same and same type and same actual type (in the backend before conversion to JSON)
===== same and same type and same actual type and same desired type (what the customer wanted)
load more comments (2 replies)
[–] majestic@sh.itjust.works 4 points 9 months ago (2 children)

As a backend developer i still dont know a shit what that means

In javascript, === does not perform type coercion when checking for equality

[–] blackn1ght@feddit.uk 5 points 9 months ago

Because in JS:

1 == "1" // true
1 === "1" // false
load more comments
view more: next ›