Well-known symbols change built-in behaviour
+price, * 2 and > ask for a number; the template literal asks for a string; + "!" gives the hint "default", which this class treats like a string.
A symbol is a unique, primitive value used as a property key that can never clash with another key; the language's own well-known symbols, such as Symbol.iterator and Symbol.toPrimitive, are hooks that let your objects change how built-in operations treat them.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Create a Temperature class whose instances give their Celsius number for +t and "21°C" in a template literal. Log both for new Temperature(21).
Add a hidden createdBy tag to an object with a symbol key, then show that JSON.stringify doesn't include it but direct access does.
Explain it without notes
What is a symbol and what problem does it solve?
What is the difference between Symbol("x") and Symbol.for("x")?
Which operations see symbol-keyed properties and which skip them?
What is Symbol.toPrimitive, and what are its three hints?
Preview
Console
Nothing logged yet.Expected console output
250
500
250 INR
250 INR!
true
[object Money]
true false
3