=== vs ==
Ok, so what’s wrong with type coercion? The problem is not type coercion per si, but the rules that are used. For instance, take a look at the following snippet:
var option1 = 0 == ''''; //true var option2 = '''' == ''0''; //false
In my opinion, the results are not intuitive. In fact, there are so many rules that come into play here, that I’ve given up on using this operator (the only times you’ll see me using it is when I haven’t used JS for a while and the old C# habits come into play…but after 1h of JS, everything starts coming back and I go back to my old === and !== operators). If you don’t believe me, then take a look at the following paragraph which was taken from the spec:
If Type(x) is the same as Type(y),then
a. If Type(x) is Undefined,return true.
b. If Type(x) is Null, return true.
c. If Type(x) is Number, then
i. If x is NaN, return false.
ii. If y is NaN, return false.
iii. If x is the same number value as y, return true.
iv. If x is +0 and y is -0, return true.
v. If x is -0 and y is +0, return true.
vi. Return false.
d. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false.
e. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
f. Return true if x and y refer to the same object. Otherwise, return false.
If I’m not mistaken, there are at least 10 rules, each with several options. Can anyone blame me for simply not caring about this operator?