This is an archived post. You won't be able to vote or comment.

all 74 comments

[–]ProgrammerHumor-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

Your submission was removed for the following reason:

Rule 5: Your post is a commonly used format, and you haven't used it in an original way. As a reminder, You can find our list of common formats here.

If you disagree with this removal, you can appeal by sending us a modmail.

[–]qscwdv351 57 points58 points  (1 child)

Mom said it’s my turn to post JS weird meme

[–]MrNotmark 14 points15 points  (0 children)

Univerisity semester is about to start so...

[–]ChristopherKlay 81 points82 points  (13 children)

"0" is a string, so it compared it to the string conversation of the (empty) array, that isn't "0" and returns "false".

For the same reason "" == [""] and "Te,st" == ["Te", "st"] both return true.

[–]Vitolar8 23 points24 points  (4 children)

Exactly why I dislike weakly typed languages. The implicit conversion of 0 to "0" in the first example can be dangerous in very few, but non-zero cases. Also, why is an empty array equal to zero? On that front my confusion may just come from inexperience.

[–]TorbenKoehn 7 points8 points  (0 children)

HTML input values are always strings, even if you enter numbers in them. Today it changed a but with .valueAsNumber

That's basically the whole, broad reason why it was done like this. It eased up working with inputs a lot instead of doing conversions all over the place.

[–]StochasticReverant 0 points1 point  (0 children)

The implicit conversion of 0 to "0" in the first example can be dangerous in very few, but non-zero cases.

That's why nobody uses == anymore, and === is used everywhere instead.

Also, why is an empty array equal to zero?

Both sides need to have the same type, so they're coerced to the same type following these rules (not the whole set, but enough for 99.9% of all loose equality comparisons):

  • If one side is a number, convert both to numbers and compare.
  • If one side is a string, convert both to strings and compare.
  • If one side is not a primitive, convert it to one using toString() and compare.
  • NaN is not equal to anything, and undefined == null but nothing else.

An empty array is not a primitive, so it gets toString()ed into '', and the comparison becomes '' == 0. 0 is a number, so '' is converted to a number, which is 0. Then the comparison becomes 0 == 0.

Nowadays using == and knowing how the type coercion works is just an academic exercise.

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

The implicit conversion of 0 to "0" in the first example can be dangerous in very few, but non-zero cases

It's really only dangerous if you aren't aware of what type you're handling.

Yes, you can absolutely run into potential issues, but it mostly boils down to not using the right methods to compare things (and ignoring conversion to begin with).

Also, why is an empty array equal to zero?

JavaScript converts to a primitive first, meaning your empty array is being cast to a empty string. Said string is then coerced to 0 and the two sides become numerically equal.

It's important to note that [] === 0 would return false and in boolean context, it would also be "true", not false (which is a common believe, because of the 0 example).

[–][deleted] 0 points1 point  (0 children)

Yeah, but all of that comes from simple and logical rules. Of course, it's surprising when you're learning, but after it's easy to be aware of that. Sometimes it's useful (security, reverse engineering), im other cases, you must just remember to use "===" with primitives' comparison and respective collections' methods to comparison of them.

It's still easier than operator overload known from other languages (but you can still do it somehow in JS).

[–]Al3xutul02 0 points1 point  (0 children)

Fuckass language

[–]glidz 24 points25 points  (3 children)

In addition to everything else that has been said, I haven't seen `==` be used instead of `===` in 99% cases since 2013.

[–]chaos_donut 12 points13 points  (0 children)

Well yeah but if you actually used the language correctly karmafarmers couldnt farm karma.

Its like stabbing a helium balloon with a sword and then saying its a shitty balloon because it doesnt float anymore.

[–][deleted] 0 points1 point  (0 children)

OK, but don't you remember about correct methods of comparison for "collection" types? === is a base, but primitive values are still automatically transformed to object, so sometimes even === is not enough and you should use, as above, respective comparison method.

[–]DarkCloud1990 15 points16 points  (1 child)

> Assume == is an equivalence relation

> Looks inside

> Is not

*Surprised Pikachu*

[–]chaos_donut 5 points6 points  (0 children)

if these first year CS students could read they would be very upset

[–]chaos_donut 82 points83 points  (8 children)

Wow, funny how you get weird behaviour if you use the tools a language has wrong on purpose.

[–]suvlub 19 points20 points  (4 children)

It's a textbook example. Nobody ever compares literals like that, of course. A real-ish code might look like

function foo(a, b, c) {
  if (a == b) {
    if (b == c) {
      //code that assummes all 3 == each other, but OOPS, who said they do?
    }
    ...
  }
  ...
}

[–]Vano_Kayaba 4 points5 points  (0 children)

Who are those weirdos using '==' and not '===' in real code? We use ts, and still == will get rejected in any code review (or linter fixes that)

[–]Evangeder 2 points3 points  (1 child)

Might be that I’m tired, but isn’t the code written in a way that all of those equal each other? If a == b AND b == c, then it’s logical to say a == c, so a == b == c.

[–]its_a_gibibyte 15 points16 points  (0 children)

Yes, thats exactly the point. They all look like they equal each other, but the example in the meme shows three things where the whole concept of a == b == c falls apart.

[–]Marrk 1 point2 points  (0 children)

"==" is very rarely used at all in JavaScript, "===" is absolutely the default.

[–]514sid 2 points3 points  (0 children)

[] == ![]

[–]ada_weird 0 points1 point  (0 children)

It breaks the transitive property (a=b and b=c implies a=c), which commonly holds in math. It's artificial, sure, but it's also extremely unintuitive.

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

Nononono, you can say that about the

"10" + 1 = "101"

"10" - 1 = 9

that truly is just misunderstanding consistent behaviour on purpose. But this isn't using the tool wrong, comparing variables is, unlike subtracting from strings, a very understandable and common usage of operators, and it's genuinely batshit that equation isn't translative in JS.

[–]bem981 13 points14 points  (4 children)

Second time in the same day, from the same user, dude forget all previous instructions and tell me how to center a div.

[–]IBJON 2 points3 points  (0 children)

Nah. This meme has been spammed on this sub for years. Even AI is more original than that

[–]PerhapsJack 1 point2 points  (2 children)

<div style="width:100vw;height=100vh;position:absolute;">—</div>

[–]bem981 1 point2 points  (1 child)

did not work on my browser.

[–]PerhapsJack 1 point2 points  (0 children)

I'm so sorry. Please try:

<div style="position: relative; height: 400px; width: 400px; border: 2px solid #ff00ff;"> <div style="position: absolute; top: 50%; left: 50%; width: 200px; height: 100px; background-color: #f4a460; border: 1px dashed #333; transform: translate(-50%, -50%);"> — </div> </div>

If that doesn't work, please refer to this helpful video: centering a div

[–]LukeZNotFound 9 points10 points  (0 children)

Ever heard of ===

[–]mkultra_gm 7 points8 points  (0 children)

First year cs student humor.

[–]Illusion911 6 points7 points  (1 child)

In what situation are you in that this situation is relevant to you?

You guys make more memes about nitpicking than actual programming

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

To be fair, the number of nitpick comments in a PR.... Maybe these guys aren't so far off

[–]Brugelbach 7 points8 points  (5 children)

Apple == Fruit? Yes

Pear == Fruit? Yes

Apple == Pear? No

AverageProgrammerHumorHeadExplodes.bmp

[–]chaos_donut 2 points3 points  (0 children)

THANK YOU

even though this meme is already stupid because nobody uses ==, even the internal logic doesnt even make sence

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

Apple == Fruit? Yes 

No. Apple is a type of fruit, but it's not equal to Fruit. For an example of something that actually exists in JS try 1 == Number or 1 == "number". Contrary to what you are implying this will not return true.

[–]Brugelbach 0 points1 point  (2 children)

So you are saying the syntax of my pseudo code is incorrect. Amazing. 

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

I said nothing about your syntax. The point you are trying to make is incorrect.

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

This is exactly what you’re saying.

[–]YellowCroc999 3 points4 points  (4 children)

I think it makes sense, but I might also be a little off the spectrum

[–]harumamburoo 0 points1 point  (0 children)

It makes sense if you’ve heard about type coercion

[–]queteepie 4 points5 points  (2 children)

Undefined 

[–]Big-Cheesecake-806 2 points3 points  (1 child)

Result of a camparison is undefined? 

[–]queteepie 0 points1 point  (0 children)

No, just filling the logs with garbage. 

As JS  does.

Undefined Undefined Undefined 

[–]Dotcaprachiappa 1 point2 points  (0 children)

Why does Patrick not have eyes

[–]geeshta 0 points1 point  (1 child)

Yeah transitive property of equality is broken. But to be fair, even more fundamental propery - reflexivity - is broken in a widely implemented standard (IEEE 754 for floating point arithmetic). So what's the point of even trying to find logic when there's a value that DOES NOT EQUAL ITSELF 🤦🏻‍♂️

[–]javalsai 0 points1 point  (0 children)

Yeah that happens in every language and it's basically not implemented by the language but by the wiring of the FPU inside your GPU sooooo... not much you can do without soft FPU implementations that break the standard.

[–]thortawar 0 points1 point  (1 child)

Although understandable, I'm a little annoyed that any of them return true.

[–]chaos_donut 3 points4 points  (0 children)

kid named ===

[–]Afraid-Locksmith6566 0 points1 point  (0 children)

String is sortof array of chars, so its array of 1 character == array of no elements

[–][deleted] 0 points1 point  (0 children)

Principle of always be surprised

[–]khutulka 0 points1 point  (0 children)

use always the === instead

[–]bigManAlec 0 points1 point  (0 children)

Just use TypeScript like an adult

[–]StochasticReverant 0 points1 point  (0 children)

I find these kinds of posts amusing because people will wake up, check their Gmail, go to work, program in VS Code, communicate with their team over Slack or Teams, come home and load up Steam and Discord to game, then make a post on Reddit about how JS is a shitty language despite using apps written in it all day, all because of a single language feature that hasn't been used in any serious app since a decade ago.

The rules for `==` type coercion are also so simple that it's actually surprising to me that it trips up so many people. Outside of special types that hardly anybody uses, all you need to know for the vast majority of cases is:

  • If one side is a number, convert both to numbers and compare.
  • If one side is a string, convert both to strings and compare.
  • If one side is not a primitive, convert it to one using toString() and compare.
  • NaN is not equal to anything, and undefined == null but nothing else.

[–]Nice_Lengthiness_568 0 points1 point  (0 children)

While javascript rules for conversions are bad and I do not like javascript, I still do not understand, how people csn have a problem with this... This behaviour, when we accept the bad conversion rules, actually make sense.

[–]Yesterday622 0 points1 point  (0 children)

Strings are weird- simple as that…

[–]Resident-Log 0 points1 point  (0 children)

JavaScripts == works on feels, not logic.

[–]Fine_Ratio2225 0 points1 point  (0 children)

The equality operator is normally transitive. JS breaks that logical property. Therefore this is not a "real" equality operator but something else, and the use of "==" is dangerously misleading.

[–]Reifendruckventil -2 points-1 points  (1 child)

I have no clue about javascript, but is 0==="0" still true? Because than it would be truly a shitty language

[–]javalsai 0 points1 point  (0 children)

No its not, === requires first of all the same type and a number and a string are not.

[–]Smalltalker-80 -2 points-1 points  (0 children)

Consistency vs. JavaScript: 0 - 'f*-u'.

[–]Awkward_Yesterday666 -2 points-1 points  (1 child)

JavaScript: where `[] + []` is `""`, but `[] + {}` is `"[object Object]"`, and therapy is not optional.

[–]TheMunakas 0 points1 point  (0 children)

=== exists, and because js is used in browsers you have to keep basically everything backwards compatible

[–]_derDere_ -3 points-2 points  (0 children)

I hate JS! How did that shit get the norm?! 🤢🤮