This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.
The Symbol.for() static method searches for existing symbols in a runtime-wide symbol registry with the given key and returns it if found. Otherwise a new symbol gets created in the global symbol registry with this key.
console.log(Symbol.for("bar") === Symbol.for("bar"));
// Expected output: true
console.log(Symbol("bar") === Symbol("bar"));
// Expected output: false
const symbol1 = Symbol.for("foo");
console.log(symbol1.toString());
// Expected output: "Symbol(foo)"
Symbol.for(key)
keyString, required. The key for the symbol (and also used for the description of the symbol).
An existing symbol with the given key if found; otherwise, a new symbol is created and returned.
In contrast to Symbol(), the Symbol.for() function creates a symbol available in a global symbol registry list. Symbol.for() does also not necessarily create a new symbol on every call, but checks first if a symbol with the given key is already present in the registry. In that case, that symbol is returned. If no symbol with the given key is found, Symbol.for() will create a new global symbol.
Symbol.for("foo"); // create a new global symbol
Symbol.for("foo"); // retrieve the already created symbol
// Same global symbol, but not locally
Symbol.for("bar") === Symbol.for("bar"); // true
Symbol("bar") === Symbol("bar"); // false
// The key is also used as the description
const sym = Symbol.for("mario");
sym.toString(); // "Symbol(mario)"
To avoid name clashes with your global symbol keys and other (library code) global symbols, it might be a good idea to prefix your symbols:
Symbol.for("mdn.foo");
Symbol.for("mdn.bar");
| Desktop | Mobile | Server | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | Bun | Deno | Node.js | |
for |
40 | 12 | 36 | 27 | 9 | 40 | 36 | 27 | 9 | 4.0 | 40 | 9 | 1.0.0 | 1.0 | 0.12.0 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for