Sub topic 5
Sharing Symbols with the Global Registry
This chapter assumes you know how Symbol() creates unique values, how symbols work as property keys, and how a symbol's description works. You should also be comfortable with ES modules or CommonJS require.
The Problem: Uniqueness Works Against Sharing
Each call to Symbol() returns a new value. That protects you from collisions, but it also means two separate pieces of code can't arrive at the same symbol on their own. If module A creates Symbol('app.theme') and module B does the same, the two symbols are different, and a property set by one is invisible to the other.
Often you can just export the symbol from one module and import it everywhere else. That breaks down in some cases:
- Two copies of the same library end up in one bundle.
- Code runs in a different realm, such as an iframe or a Node
vmcontext, and that realm has its own globals. - Unrelated packages want to agree on a protocol without depending on each other.
The global symbol registry handles these cases.
Symbol.for and Symbol.keyFor
Symbol.for(key) looks up key in a registry that the whole runtime shares. If a symbol with that key is already registered, you get that symbol back. If not, it creates one, registers it, and returns it.
const s1 = Symbol.for('app.theme');
const s2 = Symbol.for('app.theme');
console.log(s1 === s2); // true
console.log(s1.description); // "app.theme"
console.log(Symbol.keyFor(s1)); // "app.theme"
console.log(Symbol.keyFor(Symbol('x'))); // undefined
console.log(Symbol.for('x') === Symbol('x')); // falseSymbol.keyFor(sym) goes the other way. It returns the registry key for a registered symbol and undefined for any other symbol. That makes it the only reliable way to tell whether a symbol came from the registry, because a registered symbol and a plain symbol can have identical descriptions.
The Registry Crosses Realms
All realms in the same agent share one registry. A main page and its same-origin iframes are one example, and so are Node vm contexts. Plain symbols created in two realms never match, but registered ones do:
const vm = require('node:vm');
const fromOther = vm.runInNewContext("Symbol.for('app.theme')");
const plainOther = vm.runInNewContext("Symbol('app.theme')");
console.log(fromOther === Symbol.for('app.theme')); // true
console.log(plainOther === Symbol('app.theme')); // falseThe built-in well-known symbols such as Symbol.iterator are also identical across realms. They are not in the registry, though: Symbol.keyFor(Symbol.iterator) returns undefined.
Common Errors
| Mistake | What happens | Fix |
|---|---|---|
| Using a short key like `'id'` | Any other code calling `Symbol.for('id')` gets your symbol, so you are back to name collisions | Namespace keys, e.g. `'mylib.v1.id'` |
| Expecting `Symbol.for` to be private | Anyone who knows the key can get the symbol | Use an unregistered `Symbol()` when you don't want sharing |
| Passing a non-string key | The key is converted to a string, so `Symbol.for(1) === Symbol.for('1')` | Always pass explicit strings |
| Checking `description` to detect registration | Plain and registered symbols can share a description | Use `Symbol.keyFor(sym) !== undefined` |
| Calling `Symbol.for()` with no argument | The key becomes `"undefined"` | Always supply a key |
Choosing Between Symbol() and Symbol.for()
Use Symbol() for keys that one module owns and exports. Use Symbol.for() only when code that cannot share an import still has to agree on a key: duplicated library copies, other realms, or a published cross-package protocol. In those cases, document the exact key string as part of your API.
Exercise
A library is bundled twice in one app. Each copy runs const TAG = Symbol('lib.tag') and checks obj[TAG]. Objects tagged by one copy are not recognized by the other. Why, and what is the one-line fix?
Show answer
Each copy's Symbol('lib.tag') call creates a different symbol, so a property keyed by one is invisible to the other. Change it to const TAG = Symbol.for('lib.tag'); (ideally with a more specific namespaced key) so both copies get the same registered symbol.
Exercise
What does this print? const a = Symbol.for('7'); console.log(a === Symbol.for(7), Symbol.keyFor(a), Symbol.keyFor(Symbol('7')));
Show answer
It prints true "7" undefined. The number 7 is converted to the string "7", so both calls return the same registered symbol. keyFor returns its key "7". The plain symbol isn't registered, so keyFor returns undefined.