What Is the Difference Between == and === in JavaScript?
Learn the difference between == and === in JavaScript, how type coercion works, and why === is the safer, more predictable comparison operator.
Expected Interview Answer
== compares two values after converting them to a common type (loose equality), while === compares both value and type without any conversion (strict equality).
Because == coerces types before comparing, expressions like 1 == '1' or 0 == false evaluate to true, which can produce surprising bugs. === skips coercion entirely, so it only returns true when both operands already share the same type and value, making comparisons predictable. Most style guides and linters recommend always using === unless type coercion is explicitly intended.
- === avoids surprising type-coercion bugs
- Predictable, explicit comparisons improve code clarity
- Linters can enforce === as a default standard
- Reduces subtle equality bugs in conditionals
- Makes intent explicit when comparing values
AI Mentor Explanation
== is like an umpire who counts a batter as 'out' whether they're given out by the on-field call or the third umpire's review, treating both routes as equivalent. === is like a strict scorer who only logs a dismissal if it matches one exact specified method, refusing to accept the other route even if the outcome looks the same.
Step-by-Step Explanation
Step 1
== triggers coercion
Loose equality converts operands to a common type before comparing.
Step 2
=== skips coercion
Strict equality compares type and value directly, with no conversion.
Step 3
Coercion can surprise you
Expressions like '' == 0 or null == undefined return true under ==, which is often unintended.
Step 4
=== is the safer default
Most linters and style guides recommend === unless coercion is explicitly desired.
Step 5
Special cases still apply
NaN === NaN is false either way; use Number.isNaN() or Object.is() for that check.
What Interviewer Expects
- Explains == performs type coercion before comparing
- Explains === compares type and value without coercion
- Gives an example where == produces a surprising result
- Recommends === as the safer default
- Knows the NaN comparison edge case
Common Mistakes
- Saying == and === always behave the same
- Not knowing which specific values coerce unexpectedly
- Forgetting that NaN === NaN is false
- Using == out of habit without justification
Best Answer (HR Friendly)
“In JavaScript, == compares two values after converting them to match, which can cause unexpected results, while === compares both the value and the type exactly, making it the safer and more predictable choice in most code.”
Code Example
console.log(1 == '1'); // true (coerced to same type)
console.log(1 === '1'); // false (different types)
console.log(0 == false); // true (coerced)
console.log(0 === false);// false (number vs boolean)
console.log(null == undefined); // true
console.log(null === undefined); // falseFollow-up Questions
- What are all the falsy values in JavaScript?
- How does Object.is() differ from === for NaN and -0?
- Why is null == undefined true but null === undefined false?
- What does a linter rule like eqeqeq enforce?
- How does type coercion work in template literals or arithmetic operators?
MCQ Practice
1. What does 1 == '1' evaluate to?
Loose equality coerces the string '1' to the number 1 before comparing, so the result is true.
2. What does 1 === '1' evaluate to?
Strict equality does not coerce types, and a number is never strictly equal to a string, so the result is false.
3. Which operator is generally recommended by style guides?
=== is generally recommended because it avoids unpredictable type coercion.
Flash Cards
What does == do? — Compares values after converting them to a common type.
What does === do? — Compares both type and value with no coercion.
Is 0 == false true? — Yes, due to coercion; 0 === false is false.
Which is generally safer to use? — ===, since it avoids surprising coercion bugs.