What is the difference between `==` and `===` in JavaScript?
Quick answer
=== compares without converting operand types, so 0 === "0" is false. == may convert an operand before comparing, so 0 == "0" is true. The operators can therefore give different results for the same values. Use === by default for predictable comparisons; use == only when you deliberately want its conversion behavior and understand the comparison involved. [S1]
The question, the details, the next step.
A practical checklist
Identify the two operands and their expected types.
Use
===when you do not want type conversion.If using
==, check that its conversion behavior matches your intent.Compare a type-mismatch example, such as
0and"0".Make the condition’s intended behavior clear to the next reader.
A few more useful answers
Does `===` convert one operand to match the other?
No. Strict equality compares without type conversion and returns false when operand types differ. [S1]
Does `==` always give the same result as `===`?
No. Loose equality can convert operands before comparing them, as 0 == "0" shows. The operators can agree in some comparisons, but they are not interchangeable. [S1]
Is `==` forbidden in JavaScript?
No. It has conversion behavior and special cases. Prefer === for predictable comparisons, and use == only when you deliberately want the relevant loose-equality behavior. [S1]
Sources
References used for this guide.
- [S1] Equality comparisons and sameness — MDN developer.mozilla.org · Checked
