JavaScript Academy · Lesson

The Global Symbol Registry

Share symbols with Symbol.for and keyFor.

Lesson 3 of 413 steps

The Global Symbol Registry is a free JavaScript Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.

A Shared Registry

The global symbol registry lets you create symbols that are shared across your whole program by key, instead of always being unique.

const a = Symbol.for("app.id")
const b = Symbol.for("app.id")
console.log(a === b)

Symbol.for

Symbol.for looks up a symbol by a string key in the registry. If one exists it is returned; otherwise a new one is created and stored.

const token = Symbol.for("token")
console.log(token.description)

Shared Identity

Unlike plain Symbol calls, two Symbol.for calls with the same key return the very same symbol, so they compare equal.

const x = Symbol.for("shared")
const y = Symbol.for("shared")
console.log(x === y)

Contrast With Symbol

Plain Symbol always makes a new unique symbol, while Symbol.for reuses an existing registry entry for the same key.

const local1 = Symbol("k")
const local2 = Symbol("k")
const global1 = Symbol.for("k")
const global2 = Symbol.for("k")
console.log(local1 === local2)
console.log(global1 === global2)

Symbol.keyFor

Symbol.keyFor does the reverse: given a registry symbol, it returns the string key that was used to create it.

const s = Symbol.for("config")
console.log(Symbol.keyFor(s))

keyFor and Local Symbols

For a symbol not in the registry, Symbol.keyFor returns undefined, which is a way to test whether a symbol is global.

const local = Symbol("private")
const global = Symbol.for("public")
console.log(Symbol.keyFor(local))
console.log(Symbol.keyFor(global))

Cross-Module Sharing

The registry is shared even across different parts of an application, such as separate modules, making it useful for well-known coordination keys.

function getKey() { return Symbol.for("event.bus") }
console.log(getKey() === Symbol.for("event.bus"))

Naming Conventions

Because the registry is global, prefix your keys to avoid collisions with other libraries, for example using a namespace like your app name.

const a = Symbol.for("myapp.user.id")
const b = Symbol.for("myapp.user.id")
console.log(a === b)

Registry Persists

Once a key is registered, every later Symbol.for with that key returns the same symbol for the lifetime of the program.

Symbol.for("once")
const again = Symbol.for("once")
console.log(Symbol.keyFor(again))

When to Use the Registry

Use the registry when separate code needs to agree on the same symbol by name. Use plain Symbol when uniqueness and privacy matter more.

const sharedFlag = Symbol.for("feature.enabled")
const privateFlag = Symbol("feature.enabled")
console.log(sharedFlag === Symbol.for("feature.enabled"))
console.log(privateFlag === Symbol.for("feature.enabled"))

Combining With Object Keys

Registry symbols work as object keys too, allowing shared metadata that any cooperating module can read by re-fetching the symbol.

const META = Symbol.for("meta")
const obj = { [META]: "shared data" }
console.log(obj[Symbol.for("meta")])

Quick Check

Test your registry knowledge.

Recap

Recap: The global registry shares symbols by key.

  • Symbol.for reuses or creates a registry symbol
  • Same key returns the same symbol
  • Symbol.keyFor returns the key, or undefined for local symbols
  • Namespace keys to avoid clashes
const a = Symbol.for("k")
console.log(Symbol.keyFor(a))
Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “The Global Symbol Registry” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “The Global Symbol Registry”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.

What will I learn in “The Global Symbol Registry”?

Share symbols with Symbol.for and keyFor. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “The Global Symbol Registry” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating Unique Symbols
  2. Symbols as Object Keys
  3. The Global Symbol Registry
  4. Well-Known Symbols
← Back to JavaScript Academy