The Global Symbol Registry
You'll learn how to create and retrieve shared symbols that can be accessed from anywhere in your JavaScript application.
The Problem Symbols Solve (and Create)
You've learned that every Symbol() call creates a completely unique symbol. But what if you need the same symbol in different parts of your code, or even across different JavaScript realms (like iframes or web workers)? Constantly passing around symbol references would be messy.
Enter the Global Symbol Registry
JavaScript provides a global symbol registry—a special storage area where symbols can be registered with string keys and retrieved later.
Symbol.for(key) searches the global registry for a symbol with the given key. If found, it returns that symbol. If not, it creates a new symbol, registers it with that key, and returns it.
const sym1 = Symbol.for('app.userId');
const sym2 = Symbol.for('app.userId');
console.log(sym1 === sym2); // true — same symbol!
Think of it like a library catalog: if a book (symbol) with that title (key) exists, you get that exact copy. Otherwise, a new one is created and catalogued.
Symbol.keyFor(symbol) does the reverse—it takes a symbol and returns its key string from the global registry (or undefined if the symbol wasn't registered).
const globalSym = Symbol.for('shared');
console.log(Symbol.keyFor(globalSym)); // "shared"
const localSym = Symbol('not-registered');
console.log(Symbol.keyFor(localSym)); // undefined
When to Use It
Use the global registry when you need symbols to be genuinely shared across modules or realms, like defining cross-module constants.
Key Takeaway: Symbol.for() creates or retrieves shared symbols from a global registry using string keys, while Symbol.keyFor() returns the key for registered symbols—enabling symbol sharing across your entire application.