LA.NET [EN]

Aug 25

=== vs ==

Posted in Javascript      3 Comments »

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?

3 comments so far

  1. Svend
    3:00 pm - 8-25-2009

    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. Bertrand Le Roy
    7:27 pm - 8-25-2009

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

  3. Greg
    4:46 pm - 3-31-2010

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

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>