I know this is a late answer, but there seems to be some possible confusion about null
and undefined
, which IMHO is what makes ==
evil, more so that the lack of transitivity, which is bad enough. Consider:
p1.supervisor = 'Alice';
p2.supervisor = 'None';
p3.supervisor = null;
p4.supervisor = undefined;
What do these mean?
p1
has a supervisor whose name is "Alice."
p2
has a supervisor whose name is "None."
p3
explicitly, unequivocally, does not have a supervisor.
p4
may or may have a supervisor. We don't know, we don't care, we're not supposed to know (privacy issue?), as in it's none of our business.
When you use ==
you are conflating null
and undefined
which is wholly improper. The two terms mean completely different things! Saying that I don't have a supervisor simply because I refused to tell you who my supervisor is is wrong!
I understand there are programmers who don't care about this difference between null
and undefined
or choose to use these terms differently. And if your world does not use null
and undefined
correctly, or you wish to give your own interpretation to these terms, so be it. I don't think that's a good idea though.
Now by the way I have no problem with null
and undefined
both being falsy! It is perfectly okay to say
if (p.supervisor) { ... }
and then null
and undefined
would cause the code that processes the supervisor to be skipped. That is correct, because we don't know or don't have a supervisor. All good. But the two situations are not equal. This is why ==
is wrong. Again, things can be falsy and used in a duck typing sense, which is great for dynamic languages. It is proper JavaScript, Pythonic, Rubyish, etc. But again, these things are NOT equal.
And don't get me started on non-transitivity: "0x16" == 10
, 10 == "10"
but not "10" == "0x16"
. Yes, JavaScript is weakly types. Yes, it is coercive. But coerciveness should never, ever, apply to equality.
By the way, Crockford does have strong opinions. But you know what? He is correct here!
FWIW I understand that there are, and I have personally run into, situations where ==
is convenient! Like taking string input for numbers and, say, comparing to 0. However, this is hack. You have convenience as a tradeoff for an inaccurate model of the world.
TL;DR: falsiness is a great concept. It should not extend to equality.
==
by default,===
stands out and lets me know something important is going on. – Hey Jan 6 at 6:28