Strict Equality vs Loose Equality

=== uses strict equality and does not perform the ordinary type coercion associated with ==. For most application code, strict equality makes intent easier to understand. Loose equality has a defined coercion algorithm and is not random, but its behavior is sufficiently complex that it should be used deliberately.

Remember that NaN !== NaN, while Object.is(NaN, NaN) is true. Object.is also distinguishes 0 and -0.

Equality Algorithms
console.log(1 === '1');
console.log(1 == '1');
console.log(NaN === NaN);
console.log(Object.is(NaN, NaN));
console.log(Object.is(0, -0));
💡 Strict equality does not coerce 1 and '1'. Loose equality does. Object.is uses SameValue semantics, making NaN equal to itself and distinguishing positive and negative zero.
Key Takeaways
  • Prefer === for ordinary equality checks.
  • == performs a defined but sometimes surprising coercion process.
  • Object.is differs from === for NaN and signed zero.
Knowledge Check

Which expression evaluates to true?