Lesson 22 of 80
A primitive type whose only guarantee is uniqueness — used as object keys that can never accidentally collide with a string key or another symbol.
10 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 2
`Symbol("id") === Symbol("id")` — true or false?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Symbol() creates a value that is guaranteed unique — no two symbols are ever equal, even if you give them the same description.
const a = Symbol("id");
const b = Symbol("id");
a === b; // false — description is just a debug label, not an identity
const user = {
name: "Ada",
[a]: 42, // a symbol as a property key
};
Object.keys(user); // ["name"] — symbol keys are skipped
JSON.stringify(user); // '{"name":"Ada"}' — skipped here too
Symbols exist to solve one specific problem: adding a property to an object without any risk of colliding with a string key someone else already used (or will use later) — including on objects you don't fully control.
You already met this without knowing it: Symbol.iterator is exactly how the iterator protocol identifies "the method that makes this object work with for...of." for (const x of arr) is really looking up arr[Symbol.iterator].
Symbol('id') creates a value guaranteed unique — even with the same description string.
Symbol() is not a constructor — new Symbol() throws a TypeError. You always create one by calling Symbol() directly, unlike almost every other wrapper type.Symbol.for(key) is different: it looks up (or creates) a symbol in a global registry keyed by string, so two separate calls with the same key do return the same symbol — the one exception to "symbols are always unique."Object.getOwnPropertySymbols() — symbols are hidden from casual enumeration, not truly private or secure.Symbols come up less as their own topic and more as "wait, how does for...of actually know how to iterate a custom object?" — the honest answer routes straight through Symbol.iterator, connecting this lesson directly to generators.