Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

Symbol uniqueness

JavaScript symbols are primitive unique identifiers. Separate Symbol() calls stay distinct even with the same description, while Symbol.for(key) reuses a symbol from the global registry.

How it works

Calling Symbol() creates a primitive identifier that remains distinct from every other symbol, even when descriptions match. The description is only a label. If code needs the same symbol to be retrieved repeatedly by name, use Symbol.for(), which consults the global symbol registry and creates an entry when needed.

Syntax

Symbol(description)
Symbol.for(key)

Parameters

NameTypeDescription
description / keystringAn optional description or registry key used to label or retrieve a symbol.

Returns

Symbol() returns a unique primitive symbol; Symbol.for(key) returns the registry symbol associated with key, creating it if absent.

Key points

  • Symbols provide primitive identifiers intended to remain distinct.
  • Every ordinary Symbol() result has unique identity.
  • Equal descriptions do not merge symbols; each call still yields a different value.
  • Symbol.for(key) provides named lookup through the global symbol registry.
  • The registry makes repeated requests with one key return one shared symbol.

Common mistakes

  • Matching descriptions do not make symbols equal; descriptions are informational labels only.
  • Use Symbol.for(key), rather than Symbol(key), when repeated lookup by a shared name must return the same symbol.

Example: Comparing ordinary and registered symbols

const first = Symbol("id");
const second = Symbol("id");

console.log(first === second); // false
console.log(Symbol.for("shared") === Symbol.for("shared")); // true

Separate Symbol() calls remain distinct, while Symbol.for() reuses a registry entry for the same key.

When to use it

Collision-resistant object keys

Adding metadata or internal fields to objects without colliding with ordinary string property names.

Shared cross-module identity

Sharing one named symbol between modules or other code that uses the same registry key.

Stable private-style markers

Representing internal identifiers whose identity should not be recreated accidentally.

Interview questions

QWhy are two symbols with the same description not equal?

Each Symbol() call produces a distinct primitive, so Symbol("x") === Symbol("x") is false. The optional description does not participate in symbol identity.

Follow-up: How can separate parts of an application obtain the same symbol? Use Symbol.for("x"), which retrieves or creates the registry entry for that key.

QHow does Symbol.for() differ from Symbol()?

Symbol.for(key) checks the global symbol registry and returns the existing entry for that key, creating one if necessary. Repeated calls with the same key therefore return the same symbol.

Follow-up: What is the contrasting behavior of Symbol("x")? It creates a fresh symbol on every call, even when the description is identical.

QWhy use symbols as object property keys?

A symbol is a primitive value used as a unique identifier, and it can serve as an object property key. This allows code to add a property without accidentally colliding with an unrelated string-named property.

Follow-up: What should be used for cross-code shared identity? A registered symbol from Symbol.for(), accepting that its identity is intentionally shared rather than fresh.

  • Symbol()
  • Symbol.for(key)
  • global symbol registry
  • unique identifier
  • object property key

Source: Symbol type - The Modern JavaScript Tutorial. Checked 2026-10-15.