OutputNot attempted
valueOf vs toString
const money = { valueOf() { return 42; }, toString() { return '$42'; },};console.log(money + 1, `${money}`, money * 2, String(money), money == 42);Spaces and quote style don’t matter · write a thrown error as TypeError
Ctrl Enter
Show answer & explanation
The expected output comes from actually running the snippet; opening this shows it next to the explanation.
Hint "default" (+, ==) and "number" (*) call valueOf first. Hint "string" (template literals, String()) calls toString first. Symbol.toPrimitive overrides both.