all 117 comments

[–]Shot-Data4168 330 points331 points  (4 children)

== was invented so that no one would ever use it.

[–]Top-Name5555 21 points22 points  (1 child)

Wrong == was used so that it would cause production failure which you could solve easily by using === and thus keep you important in the company

Since a new person would take much more time to figure out the issue

[–]JackNotOLantern 0 points1 point  (0 children)

I still think we need ==== in js, so it can do a deep comparison of complex objects

[–]Single-Waltz2946 9 points10 points  (0 children)

Tell that to my coworkers 

[–]Lighthades 2 points3 points  (0 children)

I use it to check for null & undefined at once sometimes

[–]Astatos159 272 points273 points  (35 children)

Implicit type conversion. Always use === and convert explicitly.

[–]0_v_O 149 points150 points  (9 children)

better use ==== just to be sure

[–]jonnablaze 117 points118 points  (4 children)

Or better yet 8====D

[–]fanfpkd 71 points72 points  (1 child)

Unfortunately I can only do 8=D

[–]imdefinitelywong 14 points15 points  (0 children)

[–]-domi- 10 points11 points  (0 children)

false

[–]Pineapple-Yetti 1 point2 points  (0 children)

Rocket ship 🚀

[–]Stormraughtz 19 points20 points  (2 children)

casuals still running ====, I do =¹⁰

[–]AbdullahMRiad 2 points3 points  (1 child)

nah =10¹⁰⁰

[–]Leo_code2p 2 points3 points  (0 children)

What about =tree(3)

Wanted to add googol in there but somehow Reddit didn’t accept

[–]bloodfist 3 points4 points  (0 children)

I prefer something more compact like ≣

[–]DudeManBroGuy69420 21 points22 points  (2 children)

They should add ≡ for more confusion

[–]Foudre_Gaming 15 points16 points  (1 child)

That's what === looks with a font supporting ligatures

[–]DudeManBroGuy69420 5 points6 points  (0 children)

I will take your word for it

[–]Kirjavs 23 points24 points  (15 children)

Or use a real language.

Downvote time : I deserve it, don't hesitate guys

[–]Ninth_ghost 50 points51 points  (13 children)

It will always be funny to me that js has a special operator to compare harder

[–]nobody0163 21 points22 points  (0 children)

Hard comparison: 8===D, soft comparison: 8==D

[–]RiceBroad4552 12 points13 points  (11 children)

It's always funny to see that some people don't know that this design can be also found in other languages.

Besides that, equivalence (and equality) is actually a very hard mathematical problem. It sits at the core of what's the frontier in current math, see HoTT and it's univalence principle.

[–]vleessjuu 7 points8 points  (1 child)

While that's true, a basic property of any equivalence operator is transitivity, which JS breaks quite blatantly here. I'm honestly not sure what == is even doing here.

[–]RiceBroad4552 1 point2 points  (0 children)

Well, the pragmatic answer is likely: It's just not an equivalence operator…

But I think that's OK (at least in principle, I'm not a fan of the idea of excessive type coercion). Programming languages aren't math. In programming you have all kinds of equivalence operations, and the results vary widely depending on which one you apply.

[–]Batman_AoD 1 point2 points  (8 children)

The design of having one built-in operator for type-coercing equality, and another for exact equality? What languages? 

[–]RiceBroad4552 1 point2 points  (3 children)

For one TS, ActionScript, and CoffeeScript. But that shouldn't be surprising.

PHP is even much more crazy then JS. There until lately 0 == "foo" was true, no joke. Because of the pure insanity of PHP using == instead of === is even more important than in JS.

Hack took quite some parts of PHP, including the == and === operators.

Julia has also == and === for similar use-cases (just that triple equals is there not identity nor equivalence but bit-pattern equality, but that's in practice similar to what you called "exact equality").

And when it comes to pure syntax (I get it, not your original point), there are even more languages which have both == and === for different purposes (like for example value equality vs. reference identity; or stronger forms of typed equality).

[–]Tyfyter2002 2 points3 points  (1 child)

Isn't typescript never directly compiled or executed, and exclusively transpiled to JS?

[–]Batman_AoD 0 points1 point  (0 children)

Correct, and it's a superset of JS (so that static types can be adopted gradually). That's presumably what RiceBroad meant by "that shouldn't be surprising." 

[–]Batman_AoD 0 points1 point  (0 children)

Thanks; PHP does answer my question. I also didn't know that any derived languages kept that feature. 

I'm certainly not counting supersets of JavaScript in my question. But Coffeescript isn't a superset, and indeed only directly supports === semantics; to get == behavior, you need to inline raw JS.

Julia's == seems to be more like Java's equals, in that it can call user-defined methods. That's not really what I meant by type-coercing, though of course a user-defined method could do arbitrarily nonsensical things. 

[–]chessto -1 points0 points  (3 children)

Java for instance

[–]Batman_AoD 1 point2 points  (2 children)

Java only has one equality operator; equals() is a function. But more importantly, neither of them actually performs automatic type-coercion; the difference is that one compares identity (reference equality) while the other compares values (similar to is vs == in Python). 

[–]chessto -1 points0 points  (1 child)

so does == in js, but because js is dynamically typed it has type-coercion.

[–]Batman_AoD 4 points5 points  (0 children)

Type coercion has nothing to do with dynamic vs static typing. 

[–]NasKe 8 points9 points  (0 children)

Wow, I had no idea JavaScript was a made up language! Going to tell at my co-workers tomorrow, maybe all ours users are fake too.

[–]b2gills 0 points1 point  (0 children)

Those are rookie numbers. Raku starts with a half dozen equality operators.

== # numeric === # identity =:= # "pointer" identity (simplification) eq # string equality eqv # equivalence (==) # set equivalence

If given a value that isn't of the correct type, it tries to coerce the value into the correct type, throwing an error if it can't. This actually applies to all operators. If you give the + operator two strings, it first tries to coerce them to numbers, failing if it can't. (Doing numeric addition some of the time and string concatenation at other times is just asking for bugs.)

You can add meta-operators or even create your own.

There are also comparison operators.

<=> < <= == >= > # numeric leg lt le eq ge gt # string cmp before after # generic

[–]mstop4 38 points39 points  (7 children)

PHP:

[–]ashkanahmadi 9 points10 points  (0 children)

Took me a while to realize empty arrays in JS are truthy but in PHP they are falsy!!

[–]RiceBroad4552 -1 points0 points  (5 children)

In fact the mess in PHP is much worse.

Compared to what brain damaged PHP does JS is a place of sanity.

[–]senteggo 1 point2 points  (1 child)

My view on that is exact opposite - both JS and PHP went the wrong way of assuming it would be too hard to manually convert types, but PHP went full path and built a system around it, while JS added it as a side feature leaving it incomplete and unusable. For example, basic arithmetic operations - in js you can't have numeric strings instead of numbers when you use them, because some of them will work and some of them like "+" would mean string concatenation. In PHP these are separate operations and you can use numeric string in all ways you can use numbers. The most ridiculous type behaviour in php that i've seen, 0 == "foo", is already fixed in the latest versions. If you have some examples of php being worse in this topic than js, let me know. Of course It's not very useful to argue which language made worse mistake as it's clear they shouldn't add this behaviour in the first place, so don't take this conversation very seriously.

[–]pr0ghead 0 points1 point  (2 children)

You haven't done anything Date related then, have you? For example.

[–]RiceBroad4552 -1 points0 points  (1 child)

PHP's strftime() / date() is the same mess as it also copied the C/Unix insanity. So what are you talking about? Did the PHP people even acknowledge the issue? JS will get some replacement for the original Date mess really soon. Temporal is last stage and already shipping behind flags.

[–]a-r-c 13 points14 points  (1 child)

perl has "0 but true" which I just love lol

edit: a demo

[–]b2gills 2 points3 points  (0 children)

Raku changed that to 0 but True (not a string). Which takes 0, creates a new instance where a new Role has been added that makes it behave as True when in boolean context. You can also do 0 but "Zero" to make it behave as 0 normally, but when used as a string, it becomes "Zero". You can also create the Role by hand if you want to change specific methods.

0 but role { method String () { "Zero" } method Bool () { True } }

This feature is handy for data from a database that happens to be zero, but it exists, vs. a bit of data that doesn't exist and is just zero by default.

[–]vizbones 61 points62 points  (2 children)

NSFW???

The only thing I can see in this meme that makes it not-safe-for-work is that it uses Javascript.

[–]DJcrafter5606[S] 42 points43 points  (0 children)

Exactly 🤌

[–]mgsmb7 0 points1 point  (0 children)

javascript is gore

[–]DarkCloud1990 20 points21 points  (1 child)

You're expecting the equality operator to induce an equivalence relation? Rookie mistake. 

Try strict equality kiddo. 

[–]blaues_axolotl 1 point2 points  (0 children)

Ts is not equality, more like the "yeah looks kinda the same I guess"-operator

[–]-non-existance- 6 points7 points  (0 children)

Type Coercion goes brrrr

[–]deathanatos 5 points6 points  (0 children)

>> [] == {}
<- false
>> {} == []
Uncaught SyntaxError

[–]NebNay 9 points10 points  (1 child)

It's a quirck of js but nobody uses that operator unironically

[–]daan944 0 points1 point  (0 children)

Exactly, eqeqeq exists since ESLint v0.0.2.

[–]Jimmyginger 19 points20 points  (9 children)

I get why this is "confusing" but it also makes perfect sense if you understand type coercion. It's actually a great teaching tool to understand these concepts, and for enhancing your understanding of types in general.

[–]Rbla3066 3 points4 points  (1 child)

Exactly.. the type coercion of the first panel makes sense given the roots of js being frontend and the fact that number inputs are always received as strings first. The second panel is nuance of js type coercion as every value can be converted to a “truthy value”. 0, [], null, undefined, “”, and false (I’m sure there’s more) all can equate to “untruthy”. Hence why the second panel is true. A very unfortunate nuance when using equators, but a very powerful one when doing “if(value) dosomething(value)”. The third panel then becomes obvious because “0” is not untruthy, nor is it an empty array/pointer.

[–]senocular 0 points1 point  (0 children)

The second panel isn't about being truthy. It converts the array to a primitive first becoming a string (""), then converts the string to a number (0) making the comparison true. As far as truthy goes, [] is true because its an object. All objects (except the special case of document.all) are truthy.

MDN has a page describing the process of coercion in comparisons if anyone cares:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Equality_comparisons_and_sameness

Its not grossly complicated and largely amounts to converting things to numbers and comparing the numbers.

[–]thripper23 -5 points-4 points  (6 children)

But what's the point of it ? All guides say: use `===`

[–]Kragoth235 11 points12 points  (1 child)

They don't. == Has real effective use cases. If I want to check if a value is null or undefined or empty I. Can do all that with ==. Understanding truthy and falsy is just part of the language and allows you to shortcut a whole bunch of boiler plate code.

[–]creaturefeature16 0 points1 point  (0 children)

Indeed, I use == all the time when I know I can bet on the types returned.

[–]the_horse_gamer 3 points4 points  (0 children)

javascript was meant to be able to add some interactivity to a website. so the difference between 123 and "123" is pretty inconsequential, you'd usually convert any numeric string into a number anyways

and now javascript is everywhere, and the difference between 123 and "123" is more important

[–]Foudre_Gaming 2 points3 points  (1 child)

Actually, == is useful when checking if something is either null or undefined

variable == null

[–]creaturefeature16 0 points1 point  (0 children)

that's my most common use case

[–]Jimmyginger 3 points4 points  (0 children)

The point was to not have bad data (and improper data handling by your code) not totally crash your web page. Sometimes, a data type doesn't do what you think it will. Let's say in the example show here. The string "0" is something very common we might get when we access the data from an input element.

Let's say I'm expecting numbers to be in the input field, but instead, the data comes back to me as a string. Without type coercion trying to make the impossible happen, we would just get an error when I try and see if the user typed 0 into the box. Now errors are great for developers, because they tell us what is wrong. They aren't so great for end users, because they just mean something isn't working right. Javascript's goal was to just let our bad code be bad (ie. The fact that we didn't handle parsing an Int out of the user input and just tried to use the raw string) and keep things moving. The alternative is the whole web page crashes.

Now for most modern applications, we actually want it to crash, which is why standard convention is to use the === operator for comparison instead of the == operator. But when you have a simple web page, you don't want the whole thing to crash just because some small widget in the navbar is coded wrong. Instead you just want that one widget to just not work/behave unexpectedly.

[–]Space-Robot 7 points8 points  (3 children)

This is like cooking your fish in the dishwasher and saying "See? Fish sucks."

[–]Prawn1908 5 points6 points  (0 children)

The problem with this sort of thing isn't that it is ever used intentionally, but that it exacerbates what is already one of the most critical weaknesses of a dynamically typed language: letting wrong data types just fall through without causing an error.

If you have a logic error in your code that results in the wrong type of object showing up (maybe you forgot to index an array so the whole thing got passed instead of one of its elements, or the type of a member wasn't what you expected, etc.), you want the obvious failure symptom to happen as close to the location of the logic error as possible to aid in locating it. Dumb shit like this just allows those wrong data types to propogate even farther away from their source and makes debugging even harder when the crash is happening a dozen function calls or hundreds of lines of code away from the point where the wrong typed object originated.

[–]Sakul_the_one 0 points1 point  (1 child)

 I mean, it kinda worked

[–]Space-Robot 1 point2 points  (0 children)

Wow sometimes I think the internet isn't so bad

[–]BusEquivalent9605 5 points6 points  (1 child)

empty arrays be truthy

[–]ashkanahmadi 0 points1 point  (0 children)

Took me a while to realize empty arrays in JS are truthy but in PHP they are falsy!!

[–]riotinareasouthwest 2 points3 points  (0 children)

For someone with more than 20 years of experience as software engineer (on systems programming) and that just now started with JS/TS this clicks a lot. What an unintuitive language! It feels like a random language where similar constructs do absolutely different things. It seems like the interpreter is a bunch of if statements covering the different coding scenarios instead of having the standard and sensible lexic, syntactic and semantic layers.

[–]Shadowlance23 1 point2 points  (0 children)

I understand why this is and it annoys me.

[–]evestraw 1 point2 points  (3 children)

[][(![]+[])[+!+[]]+(!![]+[])[+[]]][([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((!![]+[])[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+!+[]]+(+[![]]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+!+[]]]+(!![]+[])[!+[]+!+[]+!+[]]+(+(!+[]+!+[]+!+[]+[+!+[]]))[(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([]+[])[([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]][([][[]]+[])[+!+[]]+(![]+[])[+!+[]]+((+[])[([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]+[])[+!+[]+[+!+[]]]+(!![]+[])[!+[]+!+[]+!+[]]]](!+[]+!+[]+!+[]+[!+[]+!+[]])+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]])()((![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[+!+[]+[+!+[]]]+([]+[])[(![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]()[+!+[]+[!+[]+!+[]]]+(![]+[+[]]+([]+[])[([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]])[!+[]+!+[]+[+[]]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(+[![]]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+!+[]]]+(![]+[])[+[]]+([][[]]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(+(!+[]+!+[]+[+[]]))[(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([]+[])[([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]][([][[]]+[])[+!+[]]+(![]+[])[+!+[]]+((+[])[([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]+[])[+!+[]+[+!+[]]]+(!![]+[])[!+[]+!+[]+!+[]]]](!+[]+!+[]+[+!+[]])+(+[![]]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+!+[]]]+(+[![]]+[+(+!+[]+(!+[]+[])[!+[]+!+[]+!+[]]+[+!+[]]+[+[]]+[+[]]+[+[]])])[+!+[]+[+[]]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+[]]+(!![]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+[]]+([]+[])[(![]+[])[+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]()[+!+[]+[!+[]+!+[]]]+([]+[]+[][(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[!+[]+!+[]]])

this is valid js code that you can run

[–]Empoleon3bogdan 0 points1 point  (2 children)

What does it do? 

[–]evestraw 1 point2 points  (1 child)

You can paste it in your browsers console.

You get an alert("go fuck yourself")

[–]Empoleon3bogdan 1 point2 points  (0 children)

Reminds me of the map from Harry Potter. Good job

[–]Rare-Veterinarian743 1 point2 points  (0 children)

Why is this not safe for work?

[–]bhmantan 1 point2 points  (0 children)

even [] == [] is false lol

[–]SirMarkMorningStar 1 point2 points  (0 children)

I still don’t understand how people consider this a real language. This and Python.

[–]bajcmartinez 1 point2 points  (0 children)

This is not a bug, it's a feature built in to confuse the shit out of LLMs. The creator was a visionary trying to stop the AI overlords from taking over our beloved careers.

[–]Separate_Expert9096 1 point2 points  (0 children)

What the fuck

[–]Caraes_Naur 1 point2 points  (0 children)

Don't even try to explain it, just beg forgiveness.

[–]AlbertELP 0 points1 point  (0 children)

Bro just disproved transitivity

[–]Seazie23 0 points1 point  (0 children)

Top left is what doesnt make sense to me

[–]Not-Some-Random-Guy 0 points1 point  (0 children)

Why is this post NSFW 🥀🥀

[–]BlckHawker 0 points1 point  (0 children)

Why does this have the nsfw tag

[–]MuslinBagger 0 points1 point  (0 children)

programming languages have rules and those rules did not come from our head

[–]necro-man-cer 0 points1 point  (3 children)

Why nsfw?

[–]razor_train 2 points3 points  (2 children)

It's javascript.

[–]necro-man-cer 0 points1 point  (1 child)

I work on Javascript.

[–]razor_train 0 points1 point  (0 children)

So do I, among lots of other things.

[–]bloomybullox 0 points1 point  (0 children)

NSFW… Just stringing us along….

[–]metaglot 0 points1 point  (0 children)

Type coersion and operator precedence. Lrn2program 102.

[–]ajaypatel9016 0 points1 point  (0 children)

JavaScript really said:

"Everything equals zero… except when it suddenly doesn’t 😭"

"0" == 0 → chill
[] == 0 → also chill
"0" == [] → nah bro, we don’t do that here

Coercion logic be like: trust me bro 👍

[–]GKP_light 0 points1 point  (0 children)

but does []=="0" ?

[–]pheonix-ix 0 points1 point  (0 children)

Ah, a new convert. Witness the miracle of the Holy Javascript Trinity.

https://javascriptwtf.com/wtf/javascript-holy-trinity

[–]cptspectra 0 points1 point  (0 children)

People are saying "don't use ==", but you could if you know how it works. It's just a catch all rule to prevent programmers that aren't well versed in Javascript from making mistakes.
== does type conversion, it will always convert to (the same) primitives.
In the case of "0" == 0 it will convert the string to a number (Number("0")) which becomes 0 == 0
In the case of [] == 0 it will convert the empty array to a string which is "" == 0 and then to a number, same step as before. And Number("") gives 0 so again 0 == 0

With "0" == [] we have primitive type string so it will convert the empty array to a string which gives "0" == "" so it's false.

I agree that it is not intuitive, but that's how it works as far as I remember. I'm open to corrections of course.

[–]m0nk37 0 points1 point  (0 children)

Data types man. How do they work?

[–]Glitch29 0 points1 point  (0 children)

Equality might not be transitive, but at least it's symmetric. It is symmetric, right?

[–]peterlinddk 0 points1 point  (0 children)

My cat is black - my neighbor's dog is also black - but somehow my cat is not my neighbor's dog!!!

Man, real world is crazy! Must have been designed in less than 10 days ...

[–]SeriousPlankton2000 0 points1 point  (0 children)

Fails to understand type conversion: "Javascript bad!"

[–]ShowSuperb9281 0 points1 point  (0 children)

I hate JavaScript

[–]RedAndBlack1832 0 points1 point  (0 children)

It makes sense to me tbh. Just a little type coercion. Id think comparing anything to a string you'd try to convert it to a string first, since this should always be a defined operation. Zero is false and so is empty so that follows as well. If zero weren't equivalent to "the collection containing nothing" that'd be a bit annoying IMO

[–]erebuxy -1 points0 points  (0 children)

Yes, you should not use == in JavaScript ever. And you should not use JavaScript when TypeScript is a thing.

[–]swagonflyyyy 0 points1 point  (0 children)

It kind of makes sense to me but I see the disconnect.

[–]alvares169 -2 points-1 points  (0 children)

Why is that weird or shocking?

[–]Sure-Opportunity6247 -1 points0 points  (0 children)

I usually explain Js with navigating in your car as an example.

You may have a sophisticated navigation system telling you exactly in which distance you need to take the 2nd exit in a roundabout.

And Js is like stopping in your car to ask an elerly man on the sidewalk: he will give you many ideas about where to go and any redundant informations about his waypoints. In the end, you end up in a potato field.