always Until you fully understand the differences and implications of using the
=== operators, use the
=== operator since it will save you from obscure (non-obvious) bugs and WTFs. The “regular”
== operator can have very unexpected results due to the type-coercion internally, so using
=== is always the recommended approach.
The You Don’t Know JS series by Kyle Simpson is excellent (and free to read online). The series goes into the commonly misunderstood areas of the language and explains the “bad parts” that Crockford suggests you avoid. By understanding them you can make proper use of them and avoid the pitfalls.
The “Up & Going” book includes a section on Equality, with this specific summary of when to use the loose (
==) vs strict (
To boil down a whole lot of details to a few simple takeaways, and help you know whether to use
===in various situations, here are my simple rules:
- If either value (aka side) in a comparison could be the
- If either value in a comparison could be of these specific values (
— empty array), avoid
- In all other cases, you’re safe to use
==. Not only is it safe, but in many cases it simplifies your code in a way that improves readability.