JavaScript · The language round
What is the difference between == and ===?
Short answer
=== compares without type coercion: values must already have the same type. == applies abstract equality coercion, which makes it powerful in a few cases and surprising in many.
The short interview answer is that === checks type and value, while == converts operands to a common type before comparing. The follow-up is whether there is ever a good reason to use ==.
0 == false; // true
0 === false; // false
"" == false; // true
"" === false; // false
null == undefined; // true
null === undefined; // false
NaN === NaN; // false
Object.is(NaN, NaN); // trueThe coercion rules worth knowing
- If the types differ,
==converts booleans and strings to numbers, then compares. null == undefinedis true, but neither equals0,""orfalseunder==.- Objects are converted with
ToPrimitivebefore comparison, which is why[] == falseis true. NaNis not equal to anything, including itself, under both==and===.
The one idiomatic use of == is the null check: value == null matches both null and undefined and nothing else. Otherwise, prefer ===.
if (value == null) {
// true for null or undefined
}
if (Object.is(value, NaN)) {
// explicit NaN check
}Common mistakes
- Saying
==always converts to a boolean. It converts to a common type, not necessarily boolean. - Using
===for NaN and expecting true. UseNumber.isNaN()orObject.is(). - Treating
Object.is()as identical to===; it differs forNaNand signed zero.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1When would you intentionally use `==`?
- 2Why is `NaN === NaN` false?
- 3What is `Object.is()` and how does it differ from `===`?
- 4Why is `[] == false` true?
Say it in the interview
Triple equals compares type and value with no coercion. Double equals applies abstract equality coercion, so zero equals false and null equals undefined. I use triple equals by default because it makes types explicit. The one exception is a null check, where value == null cleanly matches both null and undefined. NaN is a separate case: it is not equal to itself under either operator.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live JavaScript mock interview and get scored feedback on depth, clarity and confidence.