Learn JavaScript
❯ types.symbols

Symbols in JavaScript

$concept

Symbols (Symbol("desc")) are guaranteed-unique identifiers — every call creates a value unequal to all others, including identically-described ones. Their job is collision-free property keys: library internals, framework hooks, and well-known protocol slots (Symbol.iterator, Symbol.toPrimitive) that user code can never accidentally clash with.

Symbols stay invisible to normal enumeration (Object.keys skips them; JSON.stringify drops them) and require computed access (obj[sym], never obj.sym). Use them for framework-level extension points and truly private-ish keys — not everyday identifiers.

$how it works
1

Unique every time

Symbol("id") !== Symbol("id") — descriptions label for debugging only; uniqueness is structural.

2

Collision-free keys

obj[Symbol("x")] = 1 can never clash with string keys or other symbols — libraries extend shared objects safely.

3

Invisible by default

Skipped by Object.keys, for...in, spread, and JSON — reach them via Object.getOwnPropertySymbols when needed.

4

Well-known protocols

Symbol.iterator, Symbol.toPrimitive, Symbol.hasInstance let classes hook iteration, conversion, and instanceof.

$example
let id = Symbol("id");
let user = { name: "Ada", [id]: 7 };
console.log(user[id], Object.keys(user).length);
Output
7 1
1The computed [id] key stores 7 under a symbol no string key can collide with.
2Direct symbol access reads 7 back normally.
3Object.keys sees only name (length 1) — the symbol hides from ordinary enumeration.
$use cases
Library-private object slots immune to user key collisions
Custom iteration via Symbol.iterator on user classes
Primitive conversion hooks with Symbol.toPrimitive
Global symbol registry sharing via Symbol.for across realms

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.