Symbol.for
Returns a shared symbol by name: the same name gives the same symbol.
Available since: ES2015
Syntax
Symbol.for(key)Parameters
| Parameter | Type | Description |
|---|---|---|
| key | string | The symbol's name in the global registry. |
Return value
symbolA symbol from the global registry — the same one for the same name.
Examples
Compared with a plain Symbol():
console.log(Symbol.for('app.id') === Symbol.for('app.id'));
console.log(Symbol('app.id') === Symbol('app.id'));Output:
true
falseNotes
The registry is shared by the whole program, even across modules and iframes. Symbol.keyFor(sym) returns the name of a registry symbol.
See also
- Symbol — Creates a unique value — for example, a key that never clashes with others.
In the tutorial
- Symbols — The symbol type in JavaScript: unique values, symbols as object keys, hidden properties, the Symbol.for global registry and built-in symbols.