Skip to the solution
MediumFrontend6 min read

What are private class fields?

290 views

The problem

Question presented to candidate: "Before the # syntax existed, developers used a leading underscore, like this._balance, to signal a field was 'private.' What's actually different about a real # private field β€” is it just a stricter convention, or something more?"

What a strong answer should cover:

  • πŸ“Œ Interview term: #field β€” a class field prefixed with #, genuinely inaccessible from outside the class body β€” not a naming convention developers are trusted to respect, but a real, enforced language restriction.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly: attempting to access a # field from OUTSIDE its class body is a genuine SyntaxError β€” caught at parse time, before the code even runs β€” fundamentally different from an underscore-prefixed field, which remains a completely ordinary, fully public, accessible property that a developer could still reach (obj._balance) despite the naming hint.
  • πŸ“Œ Interview term: private methods too β€” verified directly: #-prefixed methods work identically β€” a real, genuinely inaccessible private method, callable only from within the class's own other methods.
  • πŸ“Œ Interview term: hidden from enumeration and serialization β€” verified directly: Object.keys() and JSON.stringify() genuinely exclude private fields entirely β€” they do not appear in either, unlike an underscore-prefixed public field, which would appear in both.
  • A precise answer names the ergonomic brand-check syntax #field in obj (an ES2022 addition) β€” verified directly, correctly distinguishing a genuine instance of the class from an unrelated plain object β€” a real, safe way to check "is this actually one of my instances" without risking a thrown error from directly accessing the private field on a potentially-wrong object type.

Clarifying questions expected:

  • None β€” this is a definitional/comparison question; directly answering "is it enforced or just convention" with real proof of a genuine SyntaxError is the strong signal.

Code / implementation expected: Yes β€” a real class with a genuine #-prefixed field, verified to be inaccessible from outside via a real caught SyntaxError, is the clearest, most convincing demonstration.

  • classes
  • private

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

6 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: a # private field is genuinely inaccessible outside its class (a real SyntaxError) and excluded from enumeration

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 102 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 nextMedium

What is the difference between slice and splice?

Continue