Concepts/JavaScript

== vs === in JavaScript

== performs type coercion before comparing (loose equality), while === compares value AND type without any conversion (strict equality).

Side-by-Side Comparison

Feature== (Loose Equality)=== (Strict Equality)
Type coercionYes — converts types before comparingNo — different types always false
null == undefinedtruefalse
PredictabilityLow — memorize coercion rulesHigh — simple type + value check
PerformanceSlightly slower (coercion step)Slightly faster (no coercion)
Best practiceAvoid (except null checks)Always prefer

Code Examples

== (Loose Equality)

  • Performs type coercion before comparison
  • null == undefined is true
  • String/Boolean are converted to Number for comparison
  • Can produce surprising results with different types
0 == ''        // true ('' → 0)
0 == '0'       // true ('0' → 0)
'' == '0'      // false (same type, different values)
false == '0'   // true (false → 0, '0' → 0)
null == undefined // true (special rule)
null == 0      // false (null only == undefined)
NaN == NaN     // false (NaN is never equal to anything)

=== (Strict Equality)

  • No type coercion — types must match first
  • If types differ, immediately returns false
  • More predictable and easier to reason about
  • Recommended in virtually all cases
0 === ''        // false (number vs string)
0 === '0'       // false (number vs string)
false === '0'   // false (boolean vs string)
null === undefined // false (different types)
1 === 1         // true
'a' === 'a'     // true
NaN === NaN     // false (still!)

When to Use Which

=== (Strict Equality)

Use === by default for all comparisons. It's predictable and avoids unexpected coercion bugs.

== (Loose Equality)

Only acceptable for null/undefined checks: `value == null` catches both null and undefined in one check. ESLint's eqeqeq rule allows this specific case.

Common Mistakes

Interview Questions

Is there any legitimate use for == in modern JavaScript?

Yes, one: checking for null/undefined. `value == null` catches both null and undefined in a single check, which is more concise than `value === null || value === undefined`. Many style guides allow this specific case.

Why does [] == false evaluate to true?

The coercion chain: [] → "" (toString), "" → 0 (toNumber), false → 0 (toNumber). Then 0 == 0 is true. This is why == with objects is particularly dangerous.

How does Object.is() differ from ===?

Object.is() handles two edge cases differently: Object.is(NaN, NaN) is true (=== gives false), and Object.is(+0, -0) is false (=== gives true). React uses Object.is() internally for state comparison.

Deep Dive