Reputation: 5699
While it's clear how using the ===
operator for e.g. numbers is useful (0
, null
and undefined
all being falsy values, which can lead to confusion), I'm not sure if there are benefits to using ===
for string comparisons.
Some of my team mates use this operator for all comparisons, but does it really make sense? Is there at least some minor performance impact?
Upvotes: 4
Views: 152
Reputation: 10247
It is considered good practice to always use ===
for comparisons in JavaScript. Since JavaScript is a dynamic language, it is possible that what you think are two strings could be variables with different types.
Consider the following:
var a = "2", b = 2;
a == b // true
a === b // false
The only way to guarantee a false
result in this case would be to use ===
when comparing the two values.
Upvotes: -2
Reputation:
If you know the types are the same, then there's no difference in the algorithm between ==
and ===
.
That said, when I see ==
, I assume that I'm using it for its type coercion, which makes me stop and analyze the code.
When I see ===
I know that there's no coercion intended, so I don't need to give it a second thought.
In other words, I only use ==
if I intend for there to be some sort of type coercion.
Upvotes: 9