JavaScript · Senior · Question 61 of 75

What does Symbol.toPrimitive control?

const price = {
  [Symbol.toPrimitive](hint) { return hint === "number" ? 42 : "$42"; },
};

Explanation

The hint is "number", "string" or "default". Without it, JS calls valueOf and toString in an order depending on the hint.

Further reading

Problem with a question

JavaScript · Senior · question 61

Test yourself

Take the JavaScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz