Core Language

Symbols

A Symbol is a guaranteed-unique, immutable primitive value. Symbols serve as collision-free property keys, power the well-known protocol hooks (Symbol.iterator,Symbol.toPrimitive…), and enable truly hidden object metadata.

What is a Symbol in JavaScript?

A Symbol is a unique, immutable primitive value, mostly used as an object key that can't clash with any other key. Every Symbol() is different, even with the same description. Built-in "well-known" symbols like Symbol.iterator let you customise how your objects behave with language features such as for…of.

const id = Symbol("id");
const user = { name: "Ada", [id]: 123 };
user[id];                    // 123
Object.keys(user);           // ["name"]: symbols are skipped
Symbol("x") === Symbol("x"); // false
Symbol.for("app") === Symbol.for("app"); // true (global registry)

What is a Symbol?

  • Introduced in ES2015 — 7th primitive type (alongside string, number, bigint, boolean, null, undefined)
  • Every Symbol() call returns a unique value — even with identical descriptions
  • Immutable and non-constructable (no new Symbol())
  • Primarily used as unique property keys to avoid naming collisions
  • Symbols are NOT auto-converted to strings — Symbol + '' throws TypeError

Symbols vs String Keys

UniquenessCan collide across librariesAlways unique — guaranteed
EnumerableYes (Object.keys, for…in)No (hidden from common loops)
JSON.stringifyIncludedExcluded
DescriptionIS the keyJust a debug label
Global sharingImplicit via nameExplicit via Symbol.for()

Well-Known Symbols

JavaScript uses a fixed set of built-in symbols as hooks into language internals. Implement them on your own objects to customize how they behave with language operators.

Symbol.iteratorMakes an object iterable — enables for…of, spread, destructuring
Symbol.asyncIteratorEnables for await…of loops on async data sources
Symbol.toPrimitiveControls type coercion when object is used as number/string/default
Symbol.hasInstanceCustomizes the instanceof operator on a class
Symbol.toStringTagOverrides the tag shown in Object.prototype.toString.call()
Symbol.speciesSpecifies constructor for derived objects in map(), filter()…

Global Symbol Registry

Symbol.for(key) creates or retrieves a symbol from the runtime-wide registry. Use this when you need the same symbol across modules, scripts, or iframes — like a shared protocol. Symbol.keyFor(sym) does the reverse lookup. Local symbols (not created with Symbol.for) return undefined.

const a = Symbol.for('shared.key');
const b = Symbol.for('shared.key');
// a === b → true (same registry entry)
Symbol.keyFor(a); // 'shared.key'
const local = Symbol('x');
Symbol.keyFor(local); // undefined (not in registry)

Common Pitfalls

Using Symbol as a privacy mechanism in production
Symbols are still accessible via Object.getOwnPropertySymbols() — not truly private
new Symbol() — calling Symbol as a constructor
Symbol is not constructable; call it as a function: Symbol('desc')
Concatenating symbol with a string: sym + ''
Explicitly convert: sym.toString() or sym.description
Forgetting that JSON.stringify ignores symbol keys
If you need serialization, use string keys or a custom replacer

Interactive Examples

Code Editor

<pre id="out"></pre>

Preview

Symbols — step by step

1 / 4
1

Symbol() — guaranteed unique values

Symbol() creates a new unique value every time. Symbol('desc') includes a description for debugging only — two symbols with the same description are still different: Symbol('id') !== Symbol('id'). Symbols are primitives, not objects. They are never automatically converted to strings — attempting string interpolation throws a TypeError.

1 of 4
🔍Symbols for metadata without pollution
Libraries can attach metadata to user objects using symbol keys without risking property name collisions. The user iterating over the object with Object.keys or for...in will not see symbol-keyed properties — they act as hidden tags. This is how React attaches its fiber references to DOM nodes internally.

🎯 Quick Check

Symbol('id') === Symbol('id') — what does this evaluate to?

JavaScript Symbols: summary

Key points

  • Symbol keys don't appear in Object.keys, for…in or JSON.stringify.
  • Symbol.iterator, Symbol.asyncIterator and Symbol.toPrimitive hook into language behaviour.
  • Symbols are not private: Object.getOwnPropertySymbols can read them.

Frequently asked questions

›When would I use a Symbol?

For hidden metadata keys on objects you don't own, for constants that must be unique, and for implementing protocols like Symbol.iterator.