Lesson 9 of 9 · 26m
Coercion, Equality, and the Spec Behaviors That Bite
Type conversion rules that matter in production: equality, addition, numeric conversion, object-to-primitive, and disciplined ways to avoid accidental behavior.
JavaScript coercion is not random, but it is broad enough that memorizing examples is a losing game. The workable strategy is to understand conversion order, the few primitive operations involved, and then use linting and explicit conversion at boundaries. Senior engineers do not win by knowing every truth table; they win by making sure production code rarely depends on one.
== is an algorithm, not a synonym for equality
0 == "0" // true: string converted to number
null == undefined // true, and only loosely equal to each other
"0" == false // true through numeric conversion
[] == false // true through object-to-primitive then numeric conversionThe Abstract Equality Comparison tries different conversions depending on operand types. That is why transitive intuition fails. Use === unless comparing against both null and undefined deliberately:
if (value == null) // deliberately true for null or undefinedThat idiom is acceptable because it is explicit and narrow, not because loose equality is generally safe.
Addition is overloaded; most arithmetic is not
+ performs string concatenation if either primitive operand is a string. -, *, / force numeric conversion:
"5" + 1 // "51"
"5" - 1 // 4
true + 1 // 2At data boundaries — query strings, form values, JSON fields repaired by hand — convert explicitly:
const limit = Number(params.get("limit") ?? "50");
if (!Number.isFinite(limit)) throw new Error("invalid limit");parseInt is for parsing strings with a radix (parseInt(value, 10)), not for general numeric validation. It accepts prefixes: parseInt("10px", 10) is 10, which is either useful or catastrophic depending on intent.
Object-to-primitive is ordered
Objects convert through Symbol.toPrimitive, then valueOf, then toString depending on the hint. Dates stringify in concatenation contexts because their conversion behavior differs from plain objects. This is why unexpected strings appear in logs and URLs:
const total = { value: 10, toString() { return "10"; } };
`total=${total}`; // total=10Do not rely on accidental object conversion in APIs. Serialize explicitly with a named method or formatter.
NaN and signed zero are real data values
NaN === NaN // false
Object.is(NaN, NaN) // true
Object.is(0, -0) // falseNumber.isNaN is the safe NaN test. Object.is exists for the two edge distinctions that === deliberately treats differently. You rarely need -0 until numeric formatting, animation direction, or low-level protocols expose it; then the difference matters.
Field notes
- Enable
eqeqeqin ESLint, with the narrownullexception only if your team agrees. Boolean([])is true andBoolean("")is false; truthiness and equality are different systems.- JSON has no
undefined, NaN, Infinity, or Date object semantics. Validate after parsing rather than trusting types. - TypeScript reduces accidental coercion but does not change runtime data. Boundary validation is still a runtime job.
Was this lesson helpful?