JavaScript · Senior · Question 61 of 75
What does Symbol.toPrimitive control?
const price = {
[Symbol.toPrimitive](hint) { return hint === "number" ? 42 : "$42"; },
};- ✓Correct answer: How the object converts to a primitive in
+price,${price}and comparisons - 2Whether the object can be frozen
- 3How the object is serialized to JSON
Explanation
The hint is "number", "string" or "default". Without it, JS calls valueOf and toString in an order depending on the hint.