Javascript === vs == …What does the developer of JavaScript (Douglas Crockford) say about it?

Jamaican jerk wings, or maple chipotle?

Skippy or Jif peanut butter?

 Double equal operator in JavaScript, or triple?

Quickread: The identity (===) operator behaves the same as the equality (==) operator, but with (===) there is no type conversion. The types must be the same to be considered equal. The one-two combo of A Smarter Way to Learn JavaScript and JavaScript: The Good Parts is the killer JavaScript book combo that nails the concept.

Douglas_Crockford,_February_2013

The man. The myth. The JavaScript godfather, Douglas Crockford.

So many seemingly trivial decisions in life, but ask any serious developer (or any serious chicken wing or peanut butter aficionado, for that matter), and they will tell you: there is a difference. Today’s topic isn’t a tutorial, but rather a recap of what everybody from Mark Myers (A Smarter way to Learn JavaScript) to the developer of JavaScript himself, Douglas Crockford is saying about the == versus the ===in JavaScript code.

But don’t go in cold: in order to understand the difference, you need to have a basic understanding of JavaScript. Have a few variables and a function or two under your belt. Then, commit this to memory:

The identity (===) operator behaves the same as the equality (==) operator, but with (===) there is no type conversion. The types must be the same to be considered equal.

Also note, the == operator will compare for equality after processing the necessary type conversions. The ===operator will not do the conversion (also called type coercion). Thus, if two values are not the same type, === will return false.

Let’s hear what the developer of JavaScript has to say about this (taken from his mandatory book “JavaScript: The Good Parts”:

JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then === produces true and !== produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable. These are some of the interesting cases:

'' == '0'           // false
0 == ''             // true
0 == '0'            // true

false == 'false'    // false
false == '0'        // true

false == undefined  // false
false == null       // false
null == undefined   // true

' \t\r\n ' == 0     // true

The lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use === and !==. All of the comparisons just shown produce false with the === operator.

Perhaps the most important part of Crockford’s quote isn’t the actual point he’s trying to make, but rather his referral of == and !== as evil twins!

evil tinws

Disturbingly dead twins from the classic horror movie The Shining, known as == and !== according to Douglas Crockford.

If this is all still fuzzy to you, don’t worry.

It sounds complicated as a beginner, but you probably just need a little bit more brushing up on the basics. Check out Mark Myers’ brilliant beginner-friendly book “A Smarter Way to Learn JavaScript” then move on with Crockford’s “JavaScript: The Good Parts.” These two books are golden for nearly every foundational concept in JavaScript, and make the prospect of evil twins a little less terrifying.

       Two classic JavaScript books that belong in all developers’ libraries. 

Code On and Prosper, 
-RealToughCandy

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