Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Symbol.for

Returns a shared symbol by name: the same name gives the same symbol.

Available since: ES2015

Syntax

Symbol.for(key)

Parameters

ParameterTypeDescription
keystringThe 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
false

Notes

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.

Thanks for reading!

Donate to the site — we work hard for you.