cf.completefrontend

Predict the outputHard

Which of valueOf and toString runs for +, template literals, and *?

What does this log?
const price = {
  valueOf() {
    console.log("valueOf");
    return 42;
  },
  toString() {
    console.log("toString");
    return "$42";
  },
};
 
console.log(price + 1);
console.log(price + "");
console.log(`${price}`);
console.log(price * 2);

The answer

Work out every line it logs, in order. Then check.

Checking your sign-in…