Useful Cheatsheetsusefulcheatsheets.com
JavaScript Symbols
Chapter 35 · Page 276
Advanced

JavaScript Symbols

Use unique Symbol values as collision-free object keys and customize built-in object behavior.

TL;DR

  1. 01Instantiate unique primitive values with the built-in Symbol factory.
  2. 02Configure unique collision-free keys hidden from standard object loop enumerations.
  3. 03Use well-known symbols to customize core language behaviors like iteration.

Tips

  1. 01Use local symbols to declare properties that will never collide with third-party keys.
  2. 02Retrieve shared symbol values across realms using the global Symbol.for() registry.

Warnings

  1. 01Avoid calling Symbol using new because symbols are primitives rather than constructible classes.
  2. 02Remember that symbol properties are omitted by standard operations like JSON.stringify and loops.
Notes
Useful Cheatsheetsusefulcheatsheets.com
JavaScript Symbols
Chapter 35 · Page 277
Advanced

JavaScript Symbols

(continued)

What Symbols Are

  • Unique primitive

    Generates unique primitive values on every function call.

    const a = Symbol("id");
    const b = Symbol("id");
    console.log(a === b); // false
  • Description debugging

    Assigns debugging descriptions which do not affect symbol identity.

    const s = Symbol("label");
    console.log(s.description); // "label"
  • Primitives type

    Returns symbol from typeof checks.

    console.log(typeof Symbol()); // "symbol"
Notes
Useful Cheatsheetsusefulcheatsheets.com
JavaScript Symbols
Chapter 35 · Page 278
Advanced

JavaScript Symbols

(continued)

Object Keys

  • Symbol properties

    Assigns properties using symbol keys to guarantee collision-free attributes.

    const KEY = Symbol("key");
    const user = { name: "Ada", [KEY]: 42 };
  • Bracket accesses

    Queries symbol keys using bracket notation rather than dot accesses.

    console.log(user[KEY]); // 42
  • Descriptors listing

    Lists symbol keys using Reflect methods.

    Reflect.ownKeys(user); // ["name", Symbol(key)]
Notes
Useful Cheatsheetsusefulcheatsheets.com
JavaScript Symbols
Chapter 35 · Page 279
Advanced

JavaScript Symbols

(continued)

Omitted Enumeration

  • Loop exclusion

    Excludes symbol properties from keys listings and loops.

    const obj = { name: "A", [Symbol("id")]: 1 };
    console.log(Object.keys(obj)); // ["name"]
  • JSON serialization

    Omits symbol keys during stringify operations.

    console.log(JSON.stringify(obj)); // '{"name":"A"}'
  • Explicit queries

    Queries symbols using specialized getOwnPropertySymbols calls.

    Object.getOwnPropertySymbols(obj); // [Symbol(id)]
Notes
Useful Cheatsheetsusefulcheatsheets.com
JavaScript Symbols
Chapter 35 · Page 280
Advanced

JavaScript Symbols

(continued)

Well-Known Symbols

  • Symbol.iterator

    Enables custom iteration behavior on plain objects.

    const range = {
      [Symbol.iterator]() {
        return { next: () => ({ done: true }) };
      }
    };
    [...range];
  • Symbol.toPrimitive

    Enforces custom conversions into primitives.

    const cash = {
      amount: 50,
      [Symbol.toPrimitive](hint) {
        return hint === "string" ? `$${this.amount}` : this.amount;
      }
    };
  • Symbol.hasInstance

    Overrides instanceof check mechanics for target configurations.

    class Even {
      static [Symbol.hasInstance](num) {
        return num % 2 === 0;
      }
    }
    console.log(4 instanceof Even); // true
Notes
Useful Cheatsheetsusefulcheatsheets.com
JavaScript Symbols
Chapter 35 · Page 281
Advanced

JavaScript Symbols

(continued)

Global Registry

  • Symbol.for

    Registers shared symbol instances in a global scope index.

    const a = Symbol.for("app.id");
    const b = Symbol.for("app.id");
    console.log(a === b); // true
  • Symbol.keyFor

    Returns registered index strings matching active shared symbols.

    console.log(Symbol.keyFor(a)); // "app.id"
Notes
Useful Cheatsheetsusefulcheatsheets.com
JavaScript Symbols
Chapter 35 · Page 282
Advanced

JavaScript Symbols

(FAQ)

FAQ

A Symbol represents a unique primitive value that avoids key collisions. It allows adding custom properties to objects safely. Well-known symbols hook directly into core operations.

By design, serialization routines like JSON.stringify only process string-keyed properties. This restriction lets symbols act as hidden metadata boundaries. They remain safe from accidental log outputs.

The Symbol() factory guarantees unique instances per call. The Symbol.for() method queries the global symbol registry first. It retrieves matching instances if they already exist.

Well-known symbols are built-in hooks like Symbol.iterator. They enable objects to customize default language behaviors. This includes customizing string casting and enabling iteration protocols.

No, they are not. The Object.getOwnPropertySymbols() method returns all symbol keys from target instances. Anyone holding reference handles can access the property values.

Useful Cheatsheetsusefulcheatsheets.com
JavaScript Symbols
Chapter 35 · Page 283
Advanced

JavaScript Symbols

(In Practice)
In Practice

Private Metadata Attachment

Uses local Symbols to attach internal metadata to objects safely, preventing collision with user properties.

  1. 01Instantiate a local Symbol identifier to act as the metadata key.
  2. 02Define a setMetadata function mapping data to the object symbol key.
  3. 03Assign metadata to the object using bracket notation entries.
  4. 04Define a getMetadata function to retrieve values using the key.
  5. 05Return the helper functions while keeping the Symbol hidden.
function createMetadataSystem() {
  const METADATA = Symbol("internal_metadata");

  function setMetadata(obj, data) {
    obj[METADATA] = data;
  }

  function getMetadata(obj) {
    return obj[METADATA];
  }

  return { setMetadata, getMetadata };
}
Takeaway

Symbol properties guarantee collision-free attributes, which makes them ideal for attaching internal metadata safely.