Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Symbol.for() — symbols from a shared registry

Symbol.for(key) looks up a symbol in the global registry by a string key. It returns the existing symbol or creates and registers a new one, allowing independent pieces of code to obtain the same symbol property key.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

Symbol.for(key)

Symbol.for(key) is a static method that returns a symbol primitive. It looks up the supplied string key in the runtime-wide global symbol registry. If the key is absent, it creates a symbol and registers it under that key. The key also serves as the symbol's description.

Unlike Symbol.for("cktis.status"), every call to Symbol("cktis.status") creates a distinct symbol: identical descriptions do not imply identical identity. The registry allows symbols to be shared across files and across realms that share the registry. It is not a persistent data-storage mechanism.

Use prefixes such as cktis.status to reduce naming collisions with libraries. A registered symbol is not a secret: other code knowing the key can retrieve it. Symbol.keyFor(symbol) returns a registered symbol's key, but returns undefined for a symbol created with Symbol().

The example reads an object property through a symbol retrieved again from the registry. MDN reports cross-browser availability for Symbol.for() since September 2015; this is not a browser-only API.

Examples

const first = Symbol.for("cktis.status");
const second = Symbol.for("cktis.status");
const local = Symbol("cktis.status");
const record = { [first]: "ready" };

console.log(first === second);
console.log(first === local);
console.log(record[second]);
console.log(Symbol.keyFor(first));
console.log(Symbol.keyFor(local));

Result

true
false
ready
cktis.status
undefined

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources