tags:

views:

488

answers:

8

As I've read somewhere it is advised to use !== and === instead.

+22  A: 

"Use the strict equality operator (===) when you want to check that the two operands are of the same type and value. Use the regular equality operator (==) if you care only about the value and the type does not matter. If, for example, one operand is the number 5 and the other operand is the string "5", standard equality operator will return true, but, since they are not of the same type, a strict equality operator will return false." http://www.webreference.com/js/column26/stricteq.html

Totty
Beat me by a few seconds. Funny that we both chose the same number for our sample.
Joel Coehoorn
+18  A: 

It depends on what your'e trying to do.

!== and === compare both value and type.

5 == "5"

The above will return true. If that's not what you intended, you should do this instead:

5 === "5"

That would return false.

Joel Coehoorn
+3  A: 

The basic reason for avoiding them is that == and != are more expensive than the strict === and !== operators. This is because of other major problem, == and != both perform an implicit toString conversion if the types of the two values being compared are different.

A trivial example is

alert({toString:function (){return "1"} == 1)

This will produce true -- as toString will be called on both values, and the string results will then be compared. In this particular case it's obviously absurd, but this can be a problem in more real world cases as well.

olliej
Performance is the least of your worries. Far more concerning is the possibility of a "false positive" with the non-type-checking versions.
Sean McMillan
+1  A: 

Well, I think the "advisement" you received is probably well intended, but not really a complete remark.

JavaScript is dynamically typed, meaning variables can change type (string, number, boolean) on the fly and without casting. The parser will not complain if you do this

var int = 3;    // Is typeof Number
int = 'haha!';  // is typeof String

But, that is not to say that JavaScript is completely type oblivious. The equality and inequality operators (== and !=) compare evaluated values, and ignore type.

The strict equality and strict inequality operators (=== and !==) DO care about type. So they will compare value AND type before determining a return value.

Read more here

http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.5/guide/expr.html#1010037

Peter Bailey
that's a common misunderstanding. variables dont "change type". on dynamic languages type is a property of values, not of variables. values have a fixed type each, and variables don't have any
Javier
Good distinction, Javier. Thanks for the clarification!
Peter Bailey
+3  A: 

=== and !== Operators.

It is almost always better to use the === and !== operators. The == and != operators do type coercion. In particular, do not use == to compare against falsy values.

From Douglas Crockford's Code Conventions for the JavaScript Programming Language

See Totty's answer for more info on this, or read Douglas Crockford's The Elements of JavaScript Style, part two: Idioms, which approaches the concepts of truthiness, falsiness and JavaScript type coercion in more detail.

Rahul
I'd like to see more about that comment on comparing against "falsy" values.
Mnebuerquo
@Mnebuerquo: Edited to add a link to another Douglas Crockford article on the topic.
Rahul
+3  A: 

The semantics of === and !== are nearly always what you mean. When you're comparing one primitive to another, you want to check if they have the same value. When you're comparing one object to another, you want to check if they reference the exact same thing.

Edit: Removed misinformation about comparing "falsy" values. I'll simply say that the == operator can't distinguish null from undefined, or 0 from "", or [0] from 0. Yes, I'm serious about that last one.

savetheclocktower
I just did 'var a = false; alert(a == null);' which returns false... I'm not sure your answer is fully accurate.
Mr. Shiny and New
You're right; thanks for the comment. I've corrected the record.
savetheclocktower
A: 

Personally, in addition to the excellently valid reasons others have stated, I just like knowing that what I'm comparing is exactly what I intend. To me, the difference between the following is important, even though they both evaluate to !myVar:

false === myVar
'undefined' === typeof myVar
Andrew Hedges
A: 

I think the guy still dont get it, so i will also explain. == check is variable is the same and does not care about the type and === cares about the type.

01