Advertisement
Intermediate JavaScript, Classes & Class Syntax

What does the # syntax mean for class members?

Short answer

The # prefix marks a truly private field or method, accessible only from inside the class body itself — not even subclasses or external code can read or call it, and accessing it from outside throws a SyntaxError, not just returning undefined.

Save JavaScript mock interview
Advertisement
AModel answer
The # prefix marks a truly private field or method, accessible only from inside the class body itself — not even subclasses or external code can read or call it, and accessing it from outside throws a SyntaxError, not just returning undefined.
class BankAccount {
  #balance = 0;
  deposit(amount) { this.#balance += amount; }
  getBalance() { return this.#balance; }
}
const acc = new BankAccount();
// acc.#balance; // SyntaxError outside the class
Real-world example
Hiding an account's internal balance so it can only be modified through controlled methods like deposit()/withdraw().
Likely follow-ups
  • How did developers fake privacy before # private fields existed?
Related topics