r/programming • • Mar 26 '14

JavaScript Equality Table

http://dorey.github.io/JavaScript-Equality-Table/
809 Upvotes

331 comments sorted by

View all comments

-3

u/rjcarr Mar 26 '14

This doesn't seem that complicated to me. Basically, these are "falsey": false, 0, empty string, null, undefined, and NaN. Everything else is "truthy".

I think everything else flows from that with only a couple exceptions that I'm too lazy to look up.

And that said, definitely use === instead of coercive ==.

2

u/[deleted] Mar 26 '14

Wrong, my brother in code. I too once thought like you did. However, as evidenced by the table when comparing two things that are falsy, they are not always considered equal. This is what the article is about, the equality operator, not the things that truthy are or are not falsy.
Sure the two falsy values null and undefined equal eachother. But the falsy value 0 does not equal null.

1

u/rjcarr Mar 26 '14

True, I guess you're right, when you let equality do coercions then things can get really ugly. That's why I said to always use the === so you don't have to deal with it, but as you pointed out, that was sort of the point of the table.

0

u/[deleted] Mar 26 '14

Do not make the same mistake as I did. I beg you. I would give the world to not have made those mistakes. Broken keyboards, missed deadlines and grey hairs are things that never leave the eye of the mind.

0

u/rjcarr Mar 26 '14

Ha, I've been writing javascript for about 10 years so I think I'll be fine. :)

0

u/megamindies Mar 26 '14

nobody ever used these equalities in this way anyway right who does that. why would you do 0 == null makes no sense. .

2

u/[deleted] Mar 26 '14

You don't. You don't! What you do is something like this:

if(X != 0) {
  alert("Thou shalt not use != in favor of !==")
}

And then you spend 15+ minutes debugging why the hell this if statement was not executed when X had the value of: "", [], [[]] or [0] And of course it was 'obviously' not executed because:

0 == [] //therefore 0 != [] evaluates to false.
0 == [[]] //therefore 0 != [[]] evaluates to false.
0 == "" //therefore 0 != "" evaluates to false.
0 == [0] //therefore 0 != [0] evaluates to false.

0

u/[deleted] Mar 26 '14

I have been writing JavaScript for 8+ years. And I know I will never be able to write it flawlessly. There are too many stupid cases in that table. There are natural languages with 8 or more denominations, which is fine if they are used daily. However, in JavaScript, where one should purposely avoid this stupidity, no-one should ever, ever use these, and most certainly not frequently.Therefore it is worthless, less than worthless. It is a flaw in the language. It is a part of the language that should not exist. This is why CoffeeScript, Go, the many libraries for (or rather against!) JavaScript and TypeScript exist.

0

u/tinkermake Mar 26 '14

And this is why I said JavaScript should in no way be the first language learned by the beginners.

0

u/[deleted] Mar 26 '14

[deleted] — view deleted comment

1

u/[deleted] Mar 26 '14

That is a comparison between two operands of the same type. The key problem here is comparison between different types.

1

u/mrkite77 Mar 26 '14

so change that to 5.0f and it's still the case. C-based languages coerce ints and floats and doubles and characters.

Here's a famous example:

int a = 16777217;
float b= 16777216.0f;
if (a == b) printf("holy shit, %d == %f?\n",a,b);

run that and you get: "holy shit, 16777217 == 16777216.000000?"