Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Classes and Modules

Class fields and private members

Reference for JavaScript class fields: public, private (#name) and static fields, initialization order, arrow-function fields and how fields differ from methods.

A class field declares a property directly in the class body, without writing this.name = value in the constructor. Every new instance gets its own copy, created before the constructor body runs (or, in a subclass, right after super() returns).

Put a # in front of the name and the field becomes private: it exists only inside the class body and cannot be read, written, listed or even detected from outside. Add static and the field belongs to the class itself rather than to instances.

JavaScript
class Account {
  owner = "unknown";      // public field
  #balance = 0;           // private field
  static count = 0;       // static field
}

Try it

JS Public and private fields

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Initialization order and arrow fields

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
class Name {
  publicField;
  publicWithValue = expression;
  #privateField = expression;
  ["computed" + "Name"] = expression;
  static staticField = expression;
  static #privateStatic = expression;

  method() {
    return this.#privateField;
  }
  static check(obj) {
    return #privateField in obj;
  }
}
  • Initializers run for each instance, top to bottom, with this set to the new object. A field without an initializer is created with the value undefined.
  • Private names must be declared in the class body. Using an undeclared #name is a SyntaxError at parse time, not a runtime error.
  • #name in obj is a brand check: true if the object has that private field, without throwing.

Usage notes

  • Fields are defined, not assigned. They are created with Object.defineProperty() semantics, so setters inherited from a parent class are not triggered by a field of the same name.
  • Fields are own properties, so Object.keys(), spreading and JSON.stringify() include public ones. Private fields never appear.
  • Methods are different. Methods live once on the prototype and are shared, whereas a field holds a separate value per instance. Use arrow-function fields when a callback needs its this bound, at the cost of one function per instance.
  • Initialization order. In a subclass, the parent's fields exist during the parent's constructor but the child's fields do not yet, as the second example shows.
  • Real privacy. #x is enforced by the language, unlike the _x naming convention. Reading obj.#x on an object without that field throws TypeError.
  • No inheritance of access. A subclass cannot read its parent's private fields, and two classes can both declare #x without clashing.
  • Not deletable or reflectable. delete this.#x is a syntax error, and Object.getOwnPropertyNames() cannot see private names.
  • Proxies do not forward private access: calling a method that uses this.#x on a Proxy of an instance throws.

Browser support

Public and private instance fields and static fields work in all current browsers. #x in obj and static blocks arrived later but are supported as well; check a compatibility table for older targets.