Public, Private & Static Class Members in JavaScript

JavaScript·2 min read·Jan 14, 2026

In JavaScript, class fields and methods can have different roles depending on how they are declared: public, private, and static.

By default, all fields and methods are public, which means that they are accessible, modifiable, and executable from both within the class and the instances of the class.

However, this behavior is not always suitable depending on context.

Private class members

A private class member is a field or method that is only accessible, modifiable, or executable from within the class itself, but not its instances.

They are generally used to hide the underlying implementation of the class and prevent accidental property changes.

Define a private member

To declare a private class member, you need to prefix it with a hash-tag symbol (#):

class ClassName {  #privateField;  #privateMethod() {    //  }}

Access a private member

Note: Trying to access a private class field from an instance of the class will result in an undefined value.

Unlock the program 🚀

Pay once, own it forever.

€79

30-day money-back guarantee

  • 13 modules
  • 113 lessons with full-code examples
  • 29 projects with commented solutions
  • All future lesson and project updates
  • Lifetime access

By submitting this form, you agree to the Terms & Conditions and Privacy Policy.