What is the difference between == and === in JavaScript?

JavaScript Fundamentals Basic Fresher1–2 Yrs Conceptual 1 min read

Quick Answer

=== (strict equality) compares value and type with no conversion. == (loose equality) coerces the operands to a common type first, which produces surprising results ('' == 0, '0' == 0, null == undefined are all true). Always use === unless you specifically want x == null to catch both null and undefined.

=== checks type and value with no conversion, so 1 === '1' is false. == converts the operands to a common type first, and that produces a lot of gotchas — empty string equals zero, '0' equals zero, null equals undefined. I use === everywhere. The one exception people accept is x == null as a shorthand that's true for both null and undefined. And separately, NaN isn't equal to itself with either operator, so you check that with Number.isNaN.

  • 1 === '1' → false; 1 == '1' → true (string coerced to number).
  • null == undefined → true, but null === undefined → false.
  • NaN === NaN → false (use Number.isNaN or Object.is).
  • 0 === -0 → true, but Object.is(0, -0) → false.
  • The one accepted == use is if (x == null) as shorthand for “null or undefined”. Linters flag everything else.