Static & Private (#) Members in JavaScript
Static members belong to the class (Counter.count, Config.load()) — shared utilities, factories, and class-wide state accessed without instances. Private fields (#balance) enforce true encapsulation: inaccessible outside the class body, invisible to subclass code and reflection, with private methods (#validate()) completing the pattern.
# privacy is hard (runtime-enforced, unlike _convention or TypeScript private), and static initialization blocks (static { ... }) run complex class-level setup. Together they express shared-vs-instance and public-vs-hidden cleanly.
Static belongs to the class
Counter.total reads without new Counter() — factories, constants, and shared state live here.
# is truly private
#balance is unreachable outside the class body — stronger than _convention, enforced at runtime.
Static blocks initialize
static { ... } runs once at class definition for computed statics — registries, caches, validation.
Subclasses excluded too
# fields stay invisible to derived classes — intentional hiding, bridged via protected-style methods when needed.
class Counter { static total = 0; #n = 0; bump() { this.#n++; Counter.total++; return this.#n; }}let c = new Counter();console.log(c.bump(), Counter.total);Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.