What are Private Fields?

Private fields and methods are class members whose names start with # (#balance, #validate()); they can only be accessed from inside the class body, and the language enforces this, unlike the old _underscore convention.

What can be private

  • Instance fields: #count = 0.
  • Instance methods: #validate() {}.
  • Getters/setters: get #total() {}.
  • Static fields and methods: static #instances = 0.

How they behave

  • Truly inaccessible from outside: obj.#field outside the class is a syntax error, and they do not appear in Object.keys, JSON.stringify, for...in or even Reflect.ownKeys.
  • Per-class, not per-object: any instance method can read #field on another instance of the same class (useful for equals methods).
  • Brand checks: #field in obj returns whether obj was created by this class, a reliable alternative to instanceof across realms.
  • Not inherited for access: subclasses cannot read the parent's private fields directly; expose protected behaviour through methods.
  • Accessing a private field on an object that does not have it throws a TypeError.

Why use them

  • Protect invariants (a bank balance changes only through deposit/withdraw).
  • Hide implementation details so you can change them without breaking users.
  • Avoid name clashes with subclass fields.

Trade-offs

Proxies and some libraries cannot intercept private fields, and they cannot be accessed for debugging in code (DevTools still shows them). For object-literal-style code, closures provide the same privacy.

Code example

js
1class BankAccount {
2 #balance = 0;
3 #history = [];
4 static #accounts = 0;
5
6 constructor(owner) {
7 this.owner = owner;
8 BankAccount.#accounts++;
9 }
10
11 #record(type, amount) {
12 this.#history.push({ type, amount });
13 }
14
15 deposit(amount) {
16 if (amount <= 0) throw new RangeError("Amount must be positive");
17 this.#balance += amount;
18 this.#record("deposit", amount);
19 return this;
20 }
21
22 withdraw(amount) {
23 if (amount > this.#balance) throw new Error("Insufficient funds");
24 this.#balance -= amount;
25 this.#record("withdraw", amount);
26 return this;
27 }
28
29 get balance() {
30 return this.#balance;
31 }
32
33 static isAccount(obj) {
34 return #balance in obj; // brand check
35 }
36
37 static get count() {
38 return BankAccount.#accounts;
39 }
40}
41
42const acc = new BankAccount("Ada").deposit(100).withdraw(30);
43console.log(acc.balance, acc.owner); // 70 Ada
44console.log(Object.keys(acc), JSON.stringify(acc)); // ["owner"] {"owner":"Ada"}
45console.log(acc["#balance"]); // undefined - not a normal property
46console.log(BankAccount.isAccount(acc), BankAccount.isAccount({ balance: 70 })); // true false
47
48try {
49 acc.withdraw(1000);
50} catch (e) {
51 console.log(e.message); // Insufficient funds
52}
53console.log(BankAccount.count); // 1

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.

Open in study app