liveBook · Manning

Symbol

As explained in The Joy of JavaScript

Symbol is the JavaScript built-in function object used to create symbol values, with static methods for symbol-related operations such as global lookup.

`Symbol` is the built-in function used to create symbol values. Call it without `new`: each call produces a unique value, even when two calls use the same description. Symbols make useful object-property keys when a name must not collide with an ordinary string key or another independently created symbol. section 7.2

A description is not an identity

`const a = Symbol('name'); const b = Symbol('name');` gives `a !== b`. An object can hold both `obj.name` and `obj[a]`; `obj[Symbol('name')]` does not retrieve the value stored at `obj[a]`. Keep the symbol reference if other code needs that property. section 7.2

Symbol keys are less visible to conventional property enumeration: `Object.keys` and `Object.getOwnPropertyNames` do not return them. They are not private, however. `Object.getOwnPropertySymbols` and `Reflect.ownKeys` can discover them. Ordinary object spread and `Object.assign` can also carry symbol-keyed properties into a copy, retaining the same symbol reference. Use symbols for collision-free contracts, not as a security boundary. section 7.2 section 7.4.1

Use `Symbol.for('version')` when separate parts of an application must agree on one key without sharing a local symbol variable. It looks up the key in the runtime-wide global symbol registry, creating the symbol if necessary; `Symbol.keyFor(symbol)` retrieves a registered symbol’s key. A symbol made with `Symbol('version')` is not registered, so `Symbol.keyFor` returns `undefined` for it. section 7.3.2

A versioned application contract

A block exposes `get [Symbol.for('version')]() { return VERSION; }`. Mining code reads `block[Symbol.for('version')]` and selects the corresponding proof-of-work module. The shared key avoids relying on an ordinary property name that might collide with other code. section 7.4.2

An opt-in serialization hook

An object can implement `[Symbol.for('toJson')]()` to return its tailored JSON representation. A shared `toJson` helper calls that method when present and otherwise falls back to `JSON.stringify(obj)`. This gives objects a serialization contract without claiming a common string-named method. section 7.4.3

Symbols also serve as static hooks for behavior associated with fundamental operations such as iteration, primitive conversion, and printing. Their broader role is interoperability: a stable, collision-free property key lets objects participate in a convention without adding another ordinary name to their interface. section 7.0 section 7.8

How The Joy of JavaScript covers it

How deep it goes
Explains creation, identity, property access, reflection, and global-registry lookup, then applies those mechanics to application contracts.
Examples
Shows duplicate descriptions yielding distinct symbols, a version-keyed mining workflow, and a symbol-keyed JSON serialization hook.
What it assumes
Builds on JavaScript objects and property access while introducing symbol creation before registry and interoperability patterns.
What you can do after
Apply it via `Symbol`, `Symbol.for`, and `Symbol.keyFor` to define collision-free keys and shared application hooks.
How it connects
Connects symbols to metaprogramming, object-property reflection, interoperability, serialization, and proof-of-work module selection.

Also explained in

Compare how 5 books explain symbol →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Joy of JavaScript. The section links point to the parts of the book it draws on.