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
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, destructuringSymbol.asyncIteratorEnables for await…of loops on async data sourcesSymbol.toPrimitiveControls type coercion when object is used as number/string/defaultSymbol.hasInstanceCustomizes the instanceof operator on a classSymbol.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.
Common Pitfalls
Interactive Examples
Code Editor
<pre id="out"></pre>Preview
Symbols — step by step
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.
🎯 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…inorJSON.stringify. Symbol.iterator,Symbol.asyncIteratorandSymbol.toPrimitivehook into language behaviour.- Symbols are not private:
Object.getOwnPropertySymbolscan 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.