=== vs ==

There are still lots of guys that don’t know that JavaScript has the === operator. The truth is that you should always use this operator instead of relying on the == operator. Btw, and since we’re talking about comparison operators, there is also a !== operator that should be used when you need to test that two values are different. The main difference between these operators is that the == and != will use type coercion when the operands are of different type (if both operands have the same type, then they both work the same way).

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?


~ by Luis Abreu on August 25, 2009.

3 Responses to “=== vs ==”

  1. After 10 years of writing *alot* of JavaScript, the == still isn”t intuitive to me. Well … slightly intuitive, which is perhaps more worrisome. === is just something you need to train your brain to.

    It”s also common to see

    if (something) {

    which wallows in the same confusing territory as == does.

  2. Couldn”t agree more. === is always what you want. The principle of least surprise dictates you use it and only it.

  3. Except if your looking for ”this” in an array of elements. === will always return false.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: