Hint
Symbol.toPrimitive gives full control over all coercion paths. Hint is "number", "string", or "default". Overrides valueOf() and toString() when present.
const obj = {
[Symbol.toPrimitive](hint) {
if (hint === 'number') return 42;
if (hint === 'string') return 'hello';
return true; // default hint
}
};
console.log(+obj);
console.log(`${obj}`);
console.log(obj + '');
console.log(obj == true);42 hello true true
Explanation: +obj: numeric hint β 42. Template literal: string hint β "hello". obj + "": default hint β true β "true". obj == true: loose eq, default hint on both, obj β true, true β 1, 1 == 1 β true.
Key Insight: Symbol.toPrimitive gives full control over all coercion paths. Hint is "number", "string", or "default". Overrides valueOf() and toString() when present.