== vs === in JavaScript
Understand == vs === in JavaScript: loose vs strict equality, type coercion, tricky examples, and why developers prefer === for predictable comparisons.
Expected Interview Answer
== is the loose equality operator that compares two values after converting them to a common type (type coercion), while === is the strict equality operator that compares both value and type without any conversion.
With ==, JavaScript may coerce operands before comparing, so 0 == '' and 5 == '5' both return true, which can hide bugs. With ===, no coercion happens: the operands must be the same type and the same value, so 5 === '5' is false. Because coercion rules are subtle and surprising, most style guides recommend using === (and !==) by default, reserving == for a few deliberate cases like loosely checking for null or undefined.
- === avoids surprising type-coercion bugs
- Makes comparisons predictable and explicit
- Improves code readability and intent
- Recommended by most linters and style guides
- Easier to reason about during debugging
AI Mentor Explanation
Strict === is like a match official who says two players are equal only if both name and jersey number match exactly. Loose == is a casual fan who calls anyone wearing number 7 'the same player', converting shirts to identities so a batter and a bowler both labelled 7 seem identical — convenient, but it hides who is really who and leads to mistaken calls.
Step-by-Step Explanation
Step 1
Check the operands
Look at the two values and their types on each side of the operator.
Step 2
=== compares strictly
Strict equality returns true only when type and value both match, with no conversion.
Step 3
== may coerce
Loose equality first converts operands toward a common type, then compares the results.
Step 4
Predict coercion
Know common cases: 5 == '5' is true, 0 == false is true, null == undefined is true.
Step 5
Prefer strict
Default to === and !== to avoid surprises, using == only for deliberate null/undefined checks.
What Interviewer Expects
- A precise definition of both operators
- Understanding of type coercion
- Concrete examples where == and === differ
- Knowing null == undefined is true but null === undefined is false
- A recommendation to prefer === and why
Common Mistakes
- Saying == and === are interchangeable
- Not knowing == performs type coercion
- Forgetting NaN === NaN is false
- Believing === compares object contents rather than references
Best Answer (HR Friendly)
“In JavaScript, == checks if two values are equal after converting them to the same type, while === checks that they are equal in both value and type without any conversion. Because == can give surprising results, developers usually prefer === so comparisons stay predictable.”
Code Example
console.log(5 == '5'); // true (string coerced to number)
console.log(5 === '5'); // false (different types)
console.log(0 == false); // true (false coerced to 0)
console.log(0 === false); // false (number vs boolean)
console.log(null == undefined); // true
console.log(null === undefined); // false
console.log(NaN === NaN); // false (NaN is never equal to itself)Follow-up Questions
- What is type coercion and when does it happen?
- Why is NaN === NaN false, and how do you check for NaN?
- When is it acceptable to use == in real code?
- How does === compare objects and arrays?
- What does the Object.is method add over ===?
MCQ Practice
1. What does 5 === '5' evaluate to?
=== compares value and type with no coercion; a number and a string are different types, so the result is false.
2. Which statement about == is correct?
== performs type coercion, converting operands to a common type before comparing their values.
3. What is the result of null == undefined?
By specification null and undefined are loosely equal, so null == undefined is true, while null === undefined is false.
Flash Cards
What does == do? — Loose equality: compares values after coercing them to a common type.
What does === do? — Strict equality: compares both value and type with no coercion.
Is 0 == false true? — Yes, because false is coerced to 0; but 0 === false is false.
Which should you prefer? — === (and !==) by default, to avoid surprising coercion bugs.