Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?
When comparing two operands of the same type for equality in JavaScript, using ==
or ===
doesn't make any conceptual difference, so I'm wondering which operator is actually faster when, like in my case, a JavaScript file has to be downloaded from a remote Internet location.
While the strict equality operator ===
may perform faster on many user agents, it also requires 8 more bits of uncompressed information to be carried along the network with the JavaScript file.
As it happens, today's average CPUs are much faster in executing several hundred conditional jumps than Internet connections are in delivering one single bit, so I'd be keen to using ==
instead of ===
and !=
instead of !==
when possible. Yet I'm confused by reading so many blogs that recommend doing the opposite.
Is there any important point I'm missing?
As you say, for comparison where both operands are guaranteed to be of the same type, the two operators are specified to perform precisely the same steps and, barring compiler optimizations, are likely to perform near enough identically. Therefore there is a slight advantage in terms of file size in using ==
over ===
in those cases.
However, some people argue that consistency is more important: ===
is usually closer to what you intend when testing equality, and only using ===
and !==
is something many people find helpful and readable. Personally, I have the opposite rule and only use ===
when there is uncertainty about the types of the operands, but I wouldn't recommend either way over the other.
If you understand the differences between strict and non-strict equality and you're confident that using ==
and !=
won't cause you or anyone else working your code any problems reading and understanding code in the future, go ahead and use them.