=== vs ==

Published Tue, Aug 25 2009 11:15

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?

Filed under:

Comments

# Svend said on Tuesday, August 25, 2009 10:00 AM

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.

# Bertrand Le Roy said on Tuesday, August 25, 2009 2:27 PM

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

# Sanjeev Agarwal said on Wednesday, August 26, 2009 6:15 AM

Daily tech links for .net and related technologies - August 25-28, 2009 Web Development Clean Web.Config

# Sanjeev Agarwal said on Wednesday, August 26, 2009 6:16 AM

Daily tech links for .net and related technologies - August 25-27, 2009 Web Development Clean Web.Config

# Greg said on Wednesday, March 31, 2010 11:46 AM

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

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above:  

Search

This Blog

Tags

Community

Archives

Syndication

Email Notifications

News




  • View Luis Abreu's profile on LinkedIn


    Follow me at Twitter

    My books

    Silverlight 4.0: Curso Completo

    ASP.NET 4.0: Curso Completo

    Portuguese LINQ book cover

    Portuguese ASP.NET 3.5 book cover

    Portuguese ASP.NET AJAX book cover

    Portuguese ASP.NET AJAX book cover