Equality (== vs ===)

medium

Complete guide with examples and interview questions · 25 min

Definition

Say it out loud in an interview — clear, simple, no textbook wording.

If someone asks the difference between == and ===, I'd say: === compares type and value with no conversion — different types, immediately false. == tries to convert the operands to a common type first, then compares.

Interviewers don't really want the textbook line. They want the rule set: same type behaves like ===; null and undefined are a special pair; a string compared to a number converts the string to a number; a boolean converts to a number first; an object compared to a primitive goes through valueOf/toString. Every "weird" result — [] == false, '' == 0 — falls straight out of these.

Senior rounds always ask the same follow-up: is == ever okay? Yes, exactly once — x == null catches null or undefined in one check, and it's false for 0 and ''. Everything else, ===.

Quick Stats
25
Minutes
medium
Difficulty
5
Interview Q's

Key takeaways

  • === compares type and value with no conversion; == converts first, then compares
  • The == rule set is small and deterministic: same type → like ===, null/undefined are a special pair, string vs number converts the string, boolean converts to a number first, object vs primitive runs ToPrimitive
  • The one defensible == is `x == null` — true for null or undefined, false for 0 and ''
  • Object.is differs from === in exactly two cases: NaN equals itself, +0 and -0 differ — React uses it for state/props comparison
  • NaN is the only value not equal to itself; check with Number.isNaN, never equality

Track your mastery

Sign in to save Q&A scores, coding challenge progress, points, and streaks across devices.

Sign in to save progress