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
undefinedvalue.
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:
-
Define a
Userclass.- Define a private
emailclass field and initialize it with the string"john.doe@mail.com". - Define a public
nameclass field and initialize it with the string"John Doe".
- Define a private
-
Declare a
uservariable and initialize it with an instance of theUserclass. -
Output the properties of the
userobject.
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
undefinedvalue. Note: When used in a static method, thethiskeyword 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:
-
Define a
BankCardclass.- Define a static
issuerandcurrencyclass fields. - Define a private
cardNumberandpinclass fields. - Define a public
holderandtypeclass fields.
- Define a static
-
Declare a
cardvariable and initialize it with an instance of theBankCardclass. -
Output the static fields of the
BankCardclass. -
Output the public properties of the
cardobject.
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