Public, Private & Static Class Members in JavaScript

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

Example

Let's consider this script:

class User {
  #email = 'john.doe@mail.com';
  name = 'John Doe';
}

let user = new User();

console.log('Email: ' + user.email);
console.log('Name: ' + user.name);

When executed, it will:

  1. Define a User class.

    1. Define a private email class field and initialize it with the string "john.doe@mail.com".
    2. Define a public name class field and initialize it with the string "John Doe".
  2. Declare a user variable and initialize it with an instance of the User class.

  3. Output the properties of the user object.

Which will produce this output:

Email: undefined
Name: John Doe

Static class members

A static class member is a field or method that belongs to the class itself and is not accessible by instances of the class.

Unlike private class members, they are mostly used to store constants and share data or functionalities across instances, rather than hiding implementation.

Define a static member

To declare a static class member, you need to prefix it with the static keyword:

class ClassName {
  static privateField;

  static privateMethod() {
    //
  }
}

Access a static member

Note: Trying to access a static class field from an instance of the class will result in an undefined value. Note: When used in a static method, the this keyword refers to the class itself and not the instance of the class.

Example

Let's consider this script:

class BankCard {
  static issuer = 'TD Canada Trust';
  static currency = 'CAD';

  #cardNumber = '4111 1111 1111 1111';
  #pin = '8520';

  type = 'debit';
  holder = 'John Doe';
}

let card = new BankCard();

console.log(BankCard);
console.log(card);

When executed, it will:

  1. Define a BankCard class.

    1. Define a static issuer and currency class fields.
    2. Define a private cardNumber and pin class fields.
    3. Define a public holder and type class fields.
  2. Declare a card variable and initialize it with an instance of the BankCard class.

  3. Output the static fields of the BankCard class.

  4. Output the public properties of the card object.

Which will produce this output:

[class BankCard] { issuer: 'TD Canada Trust', currency: 'CAD' }
BankCard { type: 'debit', holder: 'John Doe' }

Enjoying the courses?

I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:

Support BackendBrewery
Public, Private & Static Class Members in JavaScript | Backend Brewery