I'm using JSLint to go through JavaScript, and it's returning many suggestions to replace ==
(two equals signs) with ===
(three equals signs) when doing things like comparing idSele_UNVEHtype.value.length == 0
inside of an if
statement.
Is there a performance benefit to replacing ==
with ===
?
Any performance improvement would be welcomed as many comparison operators exist.
If no type conversion takes place, would there be a performance gain over ==
?
Best Answer
The strict equality operator (
===
) behaves identically to the abstract equality operator (==
) except no type conversion is done, and the types must be the same to be considered equal.Reference: JavaScript Tutorial: Comparison Operators
The
==
operator will compare for equality after doing any necessary type conversions. The===
operator will not do the conversion, so if two values are not the same type===
will simply returnfalse
. Both are equally quick.To quote Douglas Crockford's excellent JavaScript: The Good Parts,
Update
A good point was brought up by @Casebash in the comments and in @Phillipe Laybaert's answer concerning objects. For objects,
==
and===
act consistently with one another (except in a special case).The special case is when you compare a primitive with an object that evaluates to the same primitive, due to its
toString
orvalueOf
method. For example, consider the comparison of a string primitive with a string object created using theString
constructor.Here the
==
operator is checking the values of the two objects and returningtrue
, but the===
is seeing that they're not the same type and returningfalse
. Which one is correct? That really depends on what you're trying to compare. My advice is to bypass the question entirely and just don't use theString
constructor to create string objects from string literals.Reference
https://262.ecma-international.org/5.1/#sec-11.9.3