Static Methods
A static method lives on the constructor, not on instances. You call User.fromEmail(email), not user.fromEmail(). Static fields work the same way.
Detailed Explanation
Methods on the prototype are about one instance: user.describe(). Some work is about the type: parse a string into a user, compare two users, hold a shared counter, look up a registry. That work belongs on the constructor. static is the keyword that puts it there.
class User {
static fromEmail(email) {
return new User(email);
}
}
User.fromEmail is a function on User. this inside a static method is the constructor that was used to call it, which matters for inheritance: Admin.fromEmail falls through to User.fromEmail if Admin did not override it, and this will be Admin if you wrote return new this(email). That pattern is how factories stay subclass-friendly.
Static fields (static nextId = 1 or static #n = 0) are properties of the constructor. They are initialised when the class is defined, from top to bottom, after the class body is evaluated. A static field initialiser can call other static methods, and it can see the class name. It does not have an instance.
static {} is a static initialisation block, for setup that needs statements rather than a single expression. It runs once, with the same this (the constructor).
Statics are inherited along the constructor prototype chain. class Admin extends User means Admin.fromEmail === User.fromEmail until Admin defines its own. You can still write Admin.fromEmail and pass Admin as this. super.method() inside a static method talks to the parent constructor.
Nothing stops you from putting a function on a constructor without class: function User() {} then User.fromEmail = function () {}. static is the class-body spelling of that.
What it costs you: confusion with instance methods, and hidden shared state. A static cache = new Map() is one map for the process (or the module's copy of the class). It is a global. Tests must clear it. Workers and tabs do not share it. Also, const { fromEmail } = User; fromEmail() loses this, same as any other method extract.
Reach for static for factories, parsers, and type-level helpers. Reach for a module-level function when the helper does not need this or inheritance. Reach for an instance method when the work needs instance data.
Code Examples
class User {
static nextId = 1;
constructor(email) {
this.id = User.nextId;
User.nextId += 1;
this.email = email;
}
static fromEmail(email) {
return new this(email);
}
}
class Admin extends User {}
const a = User.fromEmail("ada@example.com");
const b = Admin.fromEmail("root@example.com");
console.log(a.id, a instanceof User); // => 1 true
console.log(b.id, b instanceof Admin); // => 2 trueclass Ids {
static #n = 0;
static next() {
Ids.#n += 1;
return Ids.#n;
}
}
console.log(Ids.next(), Ids.next()); // => 1 2class Mathish {
static sum(...ns) {
return ns.reduce((a, b) => a + b, 0);
}
}
console.log(Mathish.sum(1, 2, 3)); // => 6
console.log(new Mathish().sum); // => undefinedCommon Gotchas
Extracting a static method drops `this`
`const f = User.fromEmail; f("x")` fails if the body uses `this`. Call `User.fromEmail` or bind it.
Static fields are shared mutable state
a `Map` on the class is a process-wide cache. Document how tests reset it.
`new this(...)` in a static factory respects the subclass
`Admin.fromEmail` then returns an `Admin` if `fromEmail` was inherited and uses `this`.
Static blocks run once at class evaluation
they are not run per `new`. Put per-instance setup in `constructor` or instance fields.