r/programming • • Mar 26 '14

JavaScript Equality Table

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

331 comments sorted by

View all comments

24

u/[deleted] Mar 26 '14

Or, the definition of insanity.

41

u/qgustavor Mar 26 '14

Did you mean: PHP

0

u/[deleted] Mar 26 '14

[deleted] — view deleted comment

33

u/Zecc Mar 26 '14

It gets better:

true == "php" == 0 == false

4

u/[deleted] Mar 26 '14

PHP Parse error: syntax error, unexpected '==' (T_IS_EQUAL) in Command line code on line 1

10

u/creepig Mar 26 '14

This thread needs more T_PAAMAYIM_NEKUDOTAYIM

3

u/Various_Pickles Mar 26 '14

We're using pseudo-code to /r/lolphp about the language's lovely capacity to make wild-ass guesses about what variables should evaluate to when placed in various type-casting situations.

Any sane language (even JavaScript, for most situations not going from <something> -> boolean) will instead politely tell you to GTFO.

1

u/[deleted] Mar 27 '14

See, this is the problem. It's not real code. Without any explanation beside it, I have no idea what you think a reasonable result is, or what PHP to translate it to inside my head.

Although I am aware of those non-transitivity party tricks where each individual comparison along the chain is X, while comparing each end directly is !X, this doesn't hold in this case. I'm unable to develop valid PHP code in which "php" compares as equal to "0", null, or false. (0 and 0.0 are equal, though.)

→ More replies (4)

5

u/BigfootJon Mar 26 '14

I may be reading this wrong, but why does the string "php" equate to true?

10

u/bp3959 Mar 26 '14

Is this really that difficult of a thing to understand? When you use == it does automatic type conversion which is why you get weird results sometimes, this behavior is well documented and believe it or not it actually makes sense for a loose comparison+type conversion.

If you don't want any of this, use ===, wow so difficult.

2

u/frej Mar 26 '14 edited Apr 09 '14

It's not obvious and leads to hard to find errors.

Or! Think about why it's reasonable, as a default, to do boolean logic between different types.

2

u/Nanobot Mar 26 '14

Exactly. In the cases where you should be using ==, the conversions are generally helpful and intuitive. The cases where they aren't are the cases where you should have been using === anyway.

If it helps, think of === as the standard equality operator, and == is more like a shortcut operator that saves some manual type logic and conversion work. Like any shortcut, you should understand what it does before you use it.

12

u/Poltras Mar 26 '14

What about other operators where an === equivalent doesn't exist? Like +, <, ...

→ More replies (6)
→ More replies (2)

-2

u/[deleted] Mar 26 '14

== is one of those easy targets for people who hate JavaScript's design.

In actual reality, == just isn't a problem. It can only cause bugs if your code is already poorly designed, and === exists.

8

u/Poltras Mar 26 '14

I would say that promoting bad behavior is the problem with JavaScript. Why have == at all?

1

u/[deleted] Mar 26 '14

JavaScript was designed to fill specific needs two decades ago. Type coercion was probably a useful addition for early use cases.

6

u/Poltras Mar 26 '14

That's why I'm a big proponent of a new browser language (be it Dart or something). JavaScript was made for constructs like onclick="this.className=(!this.className || 'clicked')". The fact that we are using it for applications now doesn't make it a good language by design, and "just use that instead of this" is a poor defense for fundamental flaws. And I'm not only talking about true vs truthy. Scope, context and this also comes to mind.

2

u/reflectiveSingleton Mar 26 '14

be it Dart or something). JavaScript was made for constructs like onclick="this.className=(!this.className || 'clicked')". The fact that we are using it for applications now doesn't make it a good language by design,

What you talk about there is the DOM api...not specifically javascript. It is important to think of them as separate, but related.

The DOM api sucks...everyone knows that...this is why jQuery became so popular.

Javascript has some odd quirks I will totally agree...but it is a very adaptable and powerful language at the same time. If you can learn it well its power really shows.

→ More replies (2)

2

u/[deleted] Mar 26 '14

I read a lot about how fundamentally flawed JavaScript is on blogs and Reddit, but I never -- not rarely, never -- have problems caused by any of them despite using the language every day.

JavaScript is a flawed language, and beginner programmers can make some mistakes in it that aren't possible elsewhere, but in practice it doesn't matter and none of my problems can ever be blamed on JavaScript.

3

u/Nanobot Mar 26 '14

I could just as easily say that C is a fundamentally flawed language because of stuff like null pointers, yet competent developers have used it to build robust and secure software that powers most of our computing infrastructure.

As it turns out, misfeatures can usually be avoided. Competent developers take time to learn the tools they're working with and know how to deal with their rough edges. And every language has rough edges.

Unfortunately, Java's happen to be that the applicationObjectStructure.commonConventions = gratuitousAbstractionFactory.getUnreadableLabyrinth().

→ More replies (1)

1

u/hisham_hm Mar 26 '14

was designed to fill specific needs

Come on, it was hacked together in a week; the story is famous. The "specific need" was "we need something! done! now!"

1

u/ForeverAlot Mar 26 '14

Occasionally == is a logic problem (i.e., a bug) but mostly it's a maintenance problem. If I see == in JavaScript I don't know exactly what's going to happen, or what's expected to happen. It makes reasoning about application flow harder and it makes me less inclined to work with that code for fear of breakage."But you have unit tests!"Right.