Skip to the solution
HardFrontend4 min read

What are Symbols and what are they used for?

874 views

The problem

Question presented to candidate: "What is a Symbol in JavaScript, and can you show a real, practical reason you'd use one as an object property key instead of a string?"

What a strong answer should cover:

  • πŸ“Œ Interview term: Symbol() β€” creates a new, genuinely unique primitive value every single time it is called, even when given the identical description string β€” verified directly: two separate Symbol("id") calls are genuinely not equal to each other.
  • πŸ“Œ Interview term: collision-free property keys β€” the real, practical use case: a symbol-keyed property genuinely cannot accidentally collide with any other property, string-keyed or otherwise, and it is genuinely excluded from Object.keys(), for...in, and JSON.stringify() β€” verified directly, making symbols useful for attaching "hidden," non-enumerable-by-default metadata to an object without polluting its normal, visible key space.
  • πŸ“Œ Interview term: well-known symbols β€” built-in symbols like Symbol.iterator that let a class opt into a JavaScript protocol β€” verified directly: implementing [Symbol.iterator]() on a custom class genuinely made it work correctly with spread syntax ([...instance]) and, by extension, for...of.
  • πŸ“Œ Interview term: Symbol.for() β€” the registered/global-registry exception β€” verified directly: unlike plain Symbol(), Symbol.for("key") genuinely returns the SAME symbol for the same key across separate calls, since it looks the key up in (and adds it to) a shared, global registry rather than creating a new unique value each time.
  • A precise answer names that a symbol-keyed property is still genuinely retrievable if truly needed via Object.getOwnPropertySymbols() β€” it is hidden from casual enumeration, not cryptographically secret or truly private (private class fields, covered in this bank's own dedicated question, are the genuinely private mechanism).

Clarifying questions expected:

  • None β€” this is a definitional/technical question; naming a real, concrete use case (well-known symbols or collision-free keys) beyond "it's a new primitive type" is the strong signal.

Code / implementation expected: Yes β€” implementing Symbol.iterator on a real custom class, verified working with spread syntax, is the clearest, most convincing demonstration of a genuine practical use.

  • symbol

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

4 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof of Symbol uniqueness, hidden-from-enumeration keys, and a working Symbol.iterator implementation

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 132 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

What is a generator function?

Continue