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]

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)
→ More replies (12)