Private Fields
Private fields are class members named with a #. Only code in the class body that declared them can read or write them. They are not the same as underscore properties or closures.
Detailed Explanation
#count = 0 declares a private instance field. static #n = 0 declares a private static field. #audit() {} is a private method. #label = () => this.#count is a private field holding a function. The # is part of the name. There is no way to compute it with brackets: obj["#count"] is a different, public string key.
Access is lexically restricted. Inside the class, this.#count works on instances of that class. Outside the class, writing #count is a syntax error, and instance.#count from another class is a TypeError at run time if the engine even lets you write it (you cannot, unless you are in a class that declared #count). A subclass does not inherit access. class Admin extends User { read() { return this.#email; } } fails if #email was declared on User. The subclass can declare its own #email, which is a different field.
Private fields are installed on the instance when the constructor runs, after super() in a subclass. Reading #count before it is installed throws. Declaring a field without an initialiser still installs it as undefined.
The in operator has a special form: #count in obj is true when obj was constructed by this class (or otherwise received that private slot). This is the brand check. It is the correct way to detect "is this really one of ours" without a public flag.
Private fields do not appear in Object.keys, for...in, Object.getOwnPropertyNames, or Reflect.ownKeys. They are not properties in the ordinary sense. Proxy traps do not see them. JSON.stringify ignores them.
What it costs you: testability and serialization. Unit tests cannot assign #cents directly; they go through the public API or you add a package-visible helper. Spread and structured clone do not copy private slots. You cannot declare a private field dynamically. WeakMaps used to be the pattern for per-instance privacy that still worked across a function boundary; # fields replace that for class-owned state, and WeakMap remains useful when the key is an object you do not control.
Reach for # when the field is an invariant of the class. Keep public fields for data that is part of the object's contract. Do not rename a public id to #id if callers and logs need it.
Code Examples
class Counter {
#count = 0;
inc() {
this.#count += 1;
return this.#count;
}
static isCounter(value) {
return #count in value;
}
}
const c = new Counter();
console.log(c.inc()); // => 1
console.log(Counter.isCounter(c)); // => true
console.log(Counter.isCounter({})); // => false
console.log(Object.keys(c)); // => []class User {
#email;
constructor(email) {
this.#email = email;
}
get email() {
return this.#email;
}
}
class Admin extends User {
constructor(email, level) {
super(email);
this.level = level;
}
}
const ada = new Admin("ada@example.com", 1);
console.log(ada.email); // => ada@example.com
console.log(ada.level); // => 1const bag = { "#count": 9 };
console.log(bag["#count"]); // => 9 (a string key, not a private field)Common Gotchas
A subclass cannot read the parent's `#` fields
declare accessors on the parent, or keep the field public if the child is supposed to use it.
Touching `#field` on the wrong object throws `TypeError`
the brand check failed. Use `#field in obj` first if the value might be foreign.
Private fields are omitted from clone and JSON
write `toJSON()` when persistence is required.
`#name` and `"#name"` are unrelated
the quotes make a public string property that looks similar in a debugger.