Symbol identities and the global registry
Quick answer
Symbols are primitive identity values. A description labels a symbol; it does not determine equality. Use the registry only when independently written code intentionally needs a shared key.
How it works
Symbol(description) creates a fresh symbol; do not use new. Symbol.for(key) looks up a string key in the global symbol registry and creates a registered symbol if absent. Symbol.keyFor(sym) recovers a registered key or returns undefined. The registry is shared across realms within its relevant agent scope, not across unrelated processes. A symbol-keyed own property can still be discovered using Reflect.ownKeys.
const a=Symbol("stock"), b=Symbol("stock"); console.log(a===b, a.description===b.description);Expected output in Node.js:
false trueDescriptions match but identities differ.
const a=Symbol.for("bytemind.stock"); console.log(a===Symbol.for("bytemind.stock"),Symbol.keyFor(a),Symbol.keyFor(Symbol("bytemind.stock")));Expected output in Node.js:
true bytemind.stock undefinedOnly the registered symbol has a registry key.
Common mistakes
Do not use a symbol as a secret or assume a matching description implies equality. Symbol.keyFor requires a symbol, not a string.
Practice
Create two fresh symbols and two registered symbols using one label. Explain which keys could collide in an object.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Symbol identities and the global registry — Knowledge Check
Reason about the protocol or programming technique and predict the example output.
What distinguishes Symbol.for("x") from Symbol("x")?
What does Symbol.keyFor return for an unregistered symbol?
Which tool can discover own symbol keys?
What is the exact console output in Symbol identities and the global registry example 1?
const a=Symbol("stock"), b=Symbol("stock"); console.log(a===b, a.description===b.description);What is the exact console output in Symbol identities and the global registry example 2?
const a=Symbol.for("bytemind.stock"); console.log(a===Symbol.for("bytemind.stock"),Symbol.keyFor(a),Symbol.keyFor(Symbol("bytemind.stock")));