Buttons @programming.dev
76 1 ReplyLunya \ she/it @iusearchlinux.fyi
I still don't understand the
===
operatorEdit: I think a more type strict
==
? Pretty sure I understand the point of typescript now.48 1 ReplySzethFriendOfNimi @lemmy.world
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 }
129 0 ReplyQuazarOmega @lemy.lol > 1 == 1 true > 1 == '1' true > 1 === '1' false
(from node REPL)
Basically it's the real equals sign
68 0 Replyfrezik @midwest.social 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.
46 0 Replymarcos @lemmy.world Or when it is something like Prolog, where equality is inherently a messy and complex concept.
2 0 Reply
SmoothIsFast @lemmy.world It's like the ==, but there's one more =
28 0 Replykevincox @lemmy.ml
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"
22 1 ReplyFeathercrown @lemmy.world 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.
5 0 Reply
Limitless_screaming @kbin.social
==
but for JavaScript. What you don't understand is the==
of JavaScript.18 0 ReplyMikina @programming.dev 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
0462097431906509019562988736854 == 0242700999142460696437005736231
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.
18 0 Replydarcy @sh.itjust.works
that is terrifying
10 0 Replyfrezik @midwest.social If you're checking passwords, you should be using constant time string checking, anyway.
More likely, you should let your bcrypt library do it for you.
1 0 Reply
BougieBirdie @lemmy.blahaj.zone
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.
6 0 Replybobbykjack @programming.dev I'd use something like:
= becomes
== equals
=== is identical to
It's funny how everyone thinks "equals" in this context should be "identical to" when, in normal language, it doesn't really mean that at all!
3 0 Reply
ShortFuse @lemmy.world 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 againstnull
orundefined
.4 0 Replyclb92 @feddit.dk Like
==
but more strict. The==
operator will do type conversion, so0 == ''
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
4 0 Reply
Blackmist @feddit.uk
JS devs should have a font that turns == into ≈.
13 0 ReplyDrunkenPirate @feddit.de ChatGpt: 1+1≈2
12 0 ReplyOpenStars @startrek.website
Reddit: 1+1=your muther (sic, x2)
X: 1+1≈we should violently overthrow the government
4chan: nvm, I don't want to get banned for saying this one
3 1 ReplyDarkaga @kbin.social 4chan: "Gamer words"
3 1 Reply
luciole (he/him) @beehaw.org
I wish the assignment operator wasn’t the equal sign.
12 0 ReplyQuazarOmega @lemy.lol x 👈 5
19 0 Replyxedrak @kbin.social
Ok deal, but that means we need to change the equality operator to 👉👈
13 0 ReplyOpenStars @startrek.website
x 🔫 5
the pew pew principle /s
8 0 Reply
cerement @slrpnk.net
:=
10 0 ReplyMxM111 @kbin.social
That’s delayed assignment.
4 1 Reply
expr @programming.dev In Haskell, it's the same as the mathematical
=
symbol.1 0 Reply
xedrak @kbin.social
cries in PHP
11 0 Replyxmunk @sh.itjust.works I also came to represent my php breathren.
3 0 Reply
cally [he/they] @pawb.social
1+1====2!
← dreamberd developer10 0 Replygandalf_der_12te @feddit.de
Basically Java in a nutshell
9 0 Reply
sooper_dooper_roofer [none/use name] @hexbear.net eight equals equals equals equals equals equals equals equals equals capital d tilde tilde
7 0 Replyjenny_ball @lemmy.world
it depends on what your definition of is is
7 0 Replygandalf_der_12te @feddit.de
it depends on what your definition of is is
1 0 Reply
GiM @lemmy.world parseInt(0.00000000005)
5
5 0 Replypftbest @sh.itjust.works classic
1 0 Reply
Lucien [hy/hym, comrade/them] @hexbear.net
====
when4 0 Reply_edge @discuss.tchncs.de == 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)
8 0 Replypocketman_stuck @lemmy.eco.br
Lol
2 0 Replypocketman_stuck @lemmy.eco.br
Lolololol
1 0 Reply
mumblerfish @lemmy.world Mathematica also has an
===
operator. And:=
.4 0 Replylurch (he/him) @sh.itjust.works It's also very language specific, like Pascal/Delphi also have ":=" for assignments and "=" for comparison, etc
8 0 Replyzarkanian @sh.itjust.works
That makes much more sense than the other way.
3 0 Reply
Agent641 @lemmy.world What does the walrus operator do?
1 0 Replymumblerfish @lemmy.world I think it's called 'delayed assignment'. So it is almost like
=
, but you can use arguments to define functions,f[a_]:=a+2
.1 0 Reply
majestic @sh.itjust.works As a backend developer i still dont know a shit what that means
4 0 ReplyUndercoverUlrikHD @programming.dev
In javascript, === does not perform type coercion when checking for equality
9 0 Replyblackn1ght @feddit.uk
Because in JS:
1 == "1" // true 1 === "1" // false
5 0 Reply
I Cast Fist @programming.dev
Don't forget that
_.isFinite('1')
returns true ;)2 0 ReplyTyfon @programming.dev I'm JavaScript developer. I love coding WebApps. JS sucks💩.
3 1 Replytiredofsametab @kbin.social 1 + false ? (I have no idea in which order JS would evaluate things as I rarely have to touch that language much anymore)
2 0 Replythericcer @reddthat.com Any Verilog devs?
1 0 Reply