HardType Coercion💻 Output Question

Object valueOf for arithmetic, toString for template

💡

Hint

For arithmetic +: valueOf is preferred. For template literals and String(): toString is preferred.

What does this output?

const obj = {
  valueOf()  { return 42; },
  toString() { return 'obj'; },
};
console.log(obj + 1);
console.log(`${obj}`);
console.log(String(obj));

Correct Output

43
obj
obj

Why this output?

Explanation: obj+1: + with number prefers valueOf (42+1=43). Template literal prefers toString ("obj"). String() calls toString ("obj").

Key Insight: For arithmetic +: valueOf is preferred. For template literals and String(): toString is preferred.

More Type Coercion Output Questions

EasyPlus operator: string concat vs numeric addMediumLoose equality edge cases with nullEasytypeof for common valuesMediumTruthy and falsy — empty array and object are truthy

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz