What's the difference between == and === in JavaScript?
Take a moment.
Say your answer out loud or write it down first — retrieval is what makes it stick. Reveal when you're ready.
Short answer
== compares after coercing both values to a common type; === compares type and value with no conversion. Prefer === in almost all cases.
Detailed explanation
Loose equality (==) applies the Abstract Equality algorithm: null == undefined is true, numbers get compared to strings after coercion, booleans are converted to numbers, and objects are compared by reference after calling valueOf/toString. These rules produce surprising results like '' == 0 being true or '0' == false being true. Strict equality (===) returns true only when both operands share the same type and equal value — with one quirk: NaN !== NaN even under strict equality, because NaN is defined as not equal to everything including itself. Object.is() handles that edge case correctly while treating +0 and -0 as different. In day-to-day code the convention is simple: always use ===, reserving == for intentional null-or-undefined checks (value == null).
Example
'5' == 5; // true — string coerced to number
'5' === 5; // false — different types
null == undefined; // true
null === undefined; // false
NaN === NaN; // false
Object.is(NaN, NaN); // trueInterview tip
Give one concrete coercion surprise ('' == 0) rather than reciting theory, then mention Object.is for the NaN edge case — it shows real-world experience.
Common mistakes
- Saying === 'checks types' loosely — explain precisely that no coercion happens.
- Forgetting that NaN never equals anything, even itself.