As I've read somewhere it is advised to use !== and === instead.
views:
488answers:
8"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
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.
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.
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
=== 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.
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.
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
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.