Symbols

medium

Complete guide with examples and interview questions · 25 min

Definition

Say it out loud in an interview — clear, simple, no textbook wording.

If someone asks what a symbol is in JavaScript, I'd say it's a primitive that's guaranteed to be unique — every time you call Symbol(), you get a brand-new value, even if you pass the same description string twice. Its main job is giving you object keys that can never collide with anyone else's keys.

What interviewers usually probe next is the "hidden" behavior: symbol-keyed properties are skipped by for..in, Object.keys(), and JSON.stringify(), which is why libraries use them to stash metadata on objects. But — and this is the trap — they're not private. Object.getOwnPropertySymbols() and Reflect.ownKeys() find them, and spread and Object.assign copy them.

In senior rounds you'll hear about well-known symbols like Symbol.iterator and Symbol.toPrimitive — hooks into the language itself. Implement [Symbol.iterator]() on a plain object and it suddenly works with for..of. Also worth blurting out once: new Symbol() throws. Symbols are never constructed.

Quick Stats
25
Minutes
medium
Difficulty
5
Interview Q's

Key takeaways

  • Symbol() creates a guaranteed-unique primitive; the description is a debug label, not an identity
  • Never use `new Symbol()` — it throws; call Symbol() as a plain function
  • Symbol-keyed properties are skipped by for..in, Object.keys, and JSON.stringify
  • They are NOT private — getOwnPropertySymbols and Reflect.ownKeys reveal them; spread and Object.assign copy them
  • Symbol.for(key) returns the same symbol from the global registry; Symbol(key) never does
  • Well-known symbols like Symbol.iterator are protocols — implement them to hook into language behavior
  • Symbols throw TypeError on implicit string conversion — use String(sym) or sym.description

Track your mastery

Sign in to save Q&A scores, coding challenge progress, points, and streaks across devices.

Sign in to save progress