Explorer
JavaScript

Type Coercion and Equality

JavaScript Theory & Concepts

Type Coercion and Equality in JavaScript

Implicit vs explicit casting, ToPrimitive abstract operations, and strict equality.

📖 The Story & Real-World Analogy

The Airport Currency Exchange Counter

"Imagine landing in a foreign airport. You can go to an official exchange teller with a clear sign and convert your currency explicitly with exact receipt rates (`Number("42")` or `String(true)`). Or, you can buy snacks from a rogue street vendor who uses implicit, mysterious rules: if you offer them fruit and silver coins, they might melt the silver into the fruit (`"hello" + 5` becomes `"hello5"`), but if you offer to subtract, they force the fruit into a numeric scale (`"5" - 2` becomes `3`). Explicit conversion keeps you safe!"

Type coercion is the automatic or implicit conversion of values from one data type to another. Understanding ECMAScript ToPrimitive and coercion rules is critical to avoiding subtle production bugs.

⚙️ How It Works Under The Hood (Step-by-Step)
1

Strict Equality (===) vs Loose Equality (==)

`===` compares value and type without coercion. `==` applies complex abstract equality comparison algorithms, coercing operands to common types.

2

The 8 Falsy Values in JavaScript

`false`, `0`, `-0`, `0n` (BigInt), `""` (empty string), `null`, `undefined`, and `NaN`. All other values—including empty arrays `[]` and empty objects `{}`—are truthy!

3

String vs Numeric Coercion

Binary `+` operator prefers string concatenation if either operand is a string. Mathematical operators (`-`, `*`, `/`, `%`) always coerce operands to numbers.

4

ToPrimitive Object Conversion

When an object is coerced, JavaScript invokes its `[Symbol.toPrimitive](hint)`, falling back to `.valueOf()` and `.toString()`.

💻 Interactive Code Walkthrough

Dissecting classic coercion puzzles and Object.is equality:

JAVASCRIPT
// 1. Plus vs Minus Coercion
console.log("10" + 5); // "105" (String concatenation)
console.log("10" - 5); // 5 (Numeric subtraction)
console.log(true + true); // 2 (true coerces to 1)

// 2. The Famous [] == ![] Puzzle
// Step 1: ![] coerces truthy array to false
// Step 2: [] == false
// Step 3: [] coerces to "" -> "" == 0 -> 0 == 0 -> true!
console.log([] == ![]); // true

// 3. Object.is for Edge Cases
console.log(NaN === NaN); // false!
console.log(Object.is(NaN, NaN)); // true
console.log(+0 === -0); // true
console.log(Object.is(+0, -0)); // false
Console Output:
CODE
105
5
2
true
false
true
true
false
⚠️ Common Pitfalls & Interview Traps
Trap
Using == with Falsy Values

The Risk: `null == undefined` is `true`, but `null == 0` is `false`. `"" == 0` is `true`, but `"0" == false` is `true`.

The Fix: Strictly adhere to `===` and `!==`. Only use `value == null` as a shorthand to check both `null` and `undefined`.

⚡ 30-Second Quick Revision Cheat Sheet (TL;DR)
  • ✓ Always default to strict equality (`===`).
  • ✓ The only 8 falsy values: `false`, `0`, `-0`, `0n`, `""`, `null`, `undefined`, `NaN`.
  • ✓ Empty arrays `[]` and empty objects `{}` are truthy in boolean contexts.
  • ✓ `+` coerces to string if one operand is a string; `-`, `*`, `/` coerce to numbers.
  • ✓ Use `Object.is(a, b)` for accurate `NaN` and `+0 / -0` comparisons.

Finished this lesson?

Mark this chapter complete to update your learning streak and unlock the next lesson.