Skip to content

Access Modifiers

Access modifiers (also called access specifiers) determine the visibility and accessibility of class members (properties and methods). They are fundamental to encapsulation.


ModifierSame ClassSubclassExternal
public✅✅✅
protected✅✅❌
private✅❌❌

class BankAccount {
#balance = 0; // Private field (ES2020+)
deposit(amount) { this.#balance += amount; }
getBalance() { return this.#balance; }
}

  • Default to private — Make fields private; expose only what’s needed
  • Use public for API — Public members are your contract with consumers
  • Use protected sparingly — Protected creates coupling to subclasses
  • Provide getters/setters instead of making fields public

Previous Topic: Advanced Concepts → Next Topic: Static Members →