What is JavaScript's type system and coercion in advanced detail?

Answer

JavaScript's type system is dynamic (types are checked at runtime) and weakly typed (implicit coercion between types). The ToPrimitive abstract operation converts objects to primitives by calling [Symbol.toPrimitive](hint) (if defined), then valueOf(), then toString(). Hint is "number", "string", or "default". Custom coercion: class Money { [Symbol.toPrimitive](hint) { if (hint === "string") return `$\${this.amount}`; return this.amount; } }. The ToNumber operation: undefined→NaN, null→0, true→1, false→0, ""→0, "42"→42, "42px"→NaN, []→0, [3]→3, [1,2]→NaN, {}→NaN. ToString operation: null→"null", undefined→"undefined", true→"true", []→"", [1,2]→"1,2", {}→"[object Object]". Understanding these operations explains all JavaScript coercion behavior. TypeScript was created specifically to add static types and eliminate coercion bugs at compile time.