Skip to content

Chapter 26 of 33

Symbols

Use unique property keys, registries, and well-known customization hooks.

30 minutes 10 quick checksBy Subha Prasad
Lesson 26 of 33Course navigation

Lesson content

Read, practise, then check your understanding

Every Symbol(description) is unique; the description aids debugging but does not define identity. Symbols can be property keys without colliding with ordinary strings.

Protocol hooks

const internalId = Symbol("internalId");
const record = { name: "Mira", [internalId]: 42 };

const shared = Symbol.for("app.status");
console.log(Symbol.keyFor(shared));

Global-registry symbols coordinate known keys across code in one realm. Well-known symbols such as Symbol.iterator, toStringTag, and hasInstance customize language operations. Object.keys excludes symbols; use Object.getOwnPropertySymbols or Reflect.ownKeys. Symbols reduce accidental collision but are not privacy—use private fields or closures for enforced hiding.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes Symbol?
Which JavaScript term matches this description: A primitive whose created values are unique property keys.
Which statement best describes global symbol registry?
Which JavaScript term matches this description: Shared symbols obtained with Symbol.for and inspected with Symbol.keyFor.
Which statement best describes well-known symbol?
Which JavaScript term matches this description: A standardized hook such as Symbol.iterator controlling language behavior.
Which statement best describes symbol property?
Which JavaScript term matches this description: A property excluded from ordinary string-key enumeration.
Which statement best describes description?
Which JavaScript term matches this description: An optional debugging label that does not affect symbol identity.

0 of 10 checks passed

Your progress is saved on this device.