C
JavaScript/Basics/Lesson 06

Type Coercion — *The Traps of ==*

40 min·theory
This chapter
5/5

Type Coercion — *The Traps of ==*

🎯 By the End of This Lesson

After finishing this lesson, you'll be able to confidently handle the following three things.

  • ✅ How implicit conversion works with == / + / -
  • ✅ The 6 falsy values (0 · '' · null · undefined · NaN · false)
  • ✅ The exact behavior of Object.is / Number.isNaN

Keep the learning objectives as a checklist — close the lesson only once you can answer all of them.

== vs === — *Always Use ===*

The Core Rule in One Line

JavaScript's == coerces types before comparing. Unexpected results are par for the course. Always use ===.

The Shocking Behavior of ==

javascript
console.log("5" == 5);          // true   ← string → number auto-conversion
console.log(0 == false);        // true   ← boolean → number auto-conversion
console.log("" == false);       // true   ← "" → 0, false → 0 → ends up as 0 == 0
console.log(null == undefined); // true   ← special rule: these two are considered equal

console.log([] == false);       // true   ← everything equals false?!
console.log([] == ![]);         // true   ← 🤯 an array equals its own negation (a famous JS joke)

Look at that last line. An array equals its own negation. The insane result of JS implicit conversion.

=== — Safe Comparison

javascript
"5" === 5         // false (different types)
0 === false       // false
null === undefined // false

5 === 5           // true (same value + same type)

=== is true only when both value and type match. Predictable.

Why ESLint Enforces This

Most lint configurations enable the eqeqeq rule by default. New code should always use ===. Once you get used to it, seeing == starts to feel uncomfortable.

> Exception: when you want to catch both null and undefined:

javascript
if (x == null) { ... }     // null OR undefined

Only this one pattern is permitted in some code styles. Otherwise, always use ===.

Truthy / Falsy

javascript
// Falsy — 6 values that evaluate to false
if (false) {}
if (0) {}
if ("") {}
if (null) {}
if (undefined) {}
if (NaN) {}

// Everything outside these 6 is Truthy
if ([]) {}       // true!  empty arrays are truthy
if ({}) {}       // true!  empty objects are truthy
if ("0") {}      // true!  the string "0" is truthy

Use if (value) as a quick check for whether a value exists. But be aware of pitfalls like empty arrays and "0".

The Most Common Pitfalls

1. Comparing input values:

javascript
const age = prompt("Age?");    // returns a string
if (age == 18) { ... }         // "18" == 18 → true (dangerous)
if (age === 18) { ... }        // false (not what you intended)
// Fix: Number(age) === 18

2. Numbers from JSON:

javascript
// API response: { count: 5 }
const { count } = await response.json();
count === 5    // true (in most cases)

JSON preserves types, so it's generally safe. However, large integers need to be converted to bigint.

Summary

  • Always use ===. == produces unexpected results
  • Remember that empty arrays and "0" are also truthy
  • Explicitly convert input values before comparing

⚡ Try It Yourself — == vs ===

Shocking results produced by `==` through automatic conversion. Compare them against `===`.
✏️ JS 코드
📟 Console output
▶ Press the Run button
⚠️ Runs in a browser sandbox — only console.log() is supported; alert/fetch are not.

🤖 Try Prompting AI Like This

Once you understand the concepts in this lesson, you can give AI specific instructions — not vague requests like "fix this," but vocabulary-driven prompts. That's where token savings start.

  • "Add appropriate TypeScript type annotations to this variable"
  • "Unify the mixed use of === and == to === and make the intent clear"
  • "Add an unknown type + type guard to the JSON.parse result in this code"

Why This Saves Tokens

Without knowing the concepts, you'd receive an AI response and still need to ask "What does that mean?" again. That follow-up question burns tokens. Learn the concept once and the conversation ends in one round.

Type Conversion and Equality Comparison - JavaScript