Skip to solution
mediumFrontend

What are private class fields?

290 views
01

Understand 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.

classesprivate
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals/OOP interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every accessibility and enumeration claim below was actually run in Node.

1. Why

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof: a # private field is genuinely inaccessible outside its class (a real SyntaxError) and excluded from enumeration
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 102 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track