Quiz

Why might you want to create static class members in JavaScript?

Topics
JavaScriptOOP

TL;DR

Static fields and methods belong to the class constructor rather than to each instance. Use them for behavior or data conceptually associated with the type as a whole: named factories, validation helpers, registries, constants, or counters. Access them as ClassName.member (or through this inside a static method), not through an instance.

Do not use static mutable state for request/user data or browser secrets. It is shared within that constructor's runtime scope, complicates isolation and concurrency, and is duplicated across processes, workers, realms, or separate module copies. A module-level function or dependency-injected object is often simpler when no class abstraction is needed.


Static versus instance members

class Temperature {
static ABSOLUTE_ZERO_C = -273.15;
constructor(celsius) {
this.celsius = celsius;
}
static fromFahrenheit(fahrenheit) {
return new Temperature(((fahrenheit - 32) * 5) / 9);
}
isPhysicallyPossible() {
return this.celsius >= Temperature.ABSOLUTE_ZERO_C;
}
}
const freezing = Temperature.fromFahrenheit(32);
console.log(freezing.celsius); // 0
console.log(freezing.isPhysicallyPossible()); // true
console.log(freezing.fromFahrenheit); // undefined

The factory belongs to the type because it creates an instance from another representation. isPhysicallyPossible() belongs to an instance because it reads that instance's celsius value.

Common uses

Named factories and parsing

Static methods can make construction intent explicit, such as Date.fromTimestamp() in an application class or UserId.parse(value). They can validate input before returning an instance and may return a subclass when called through this if designed that way.

Type-wide registries or counters

class Job {
static #nextId = 1;
constructor() {
this.id = Job.#nextId;
Job.#nextId += 1;
}
}
console.log(new Job().id); // 1
console.log(new Job().id); // 2

This is suitable for a local diagnostic identifier, not a globally unique database ID. Multiple tabs, workers, processes, or restarts each have their own counter.

Constants associated with a type

A static field can keep a domain constant near the related class. Freeze objects when callers must not mutate the shared object, and remember that static itself does not make a field immutable.

Tradeoffs

  • Static mutable state persists between calls and tests unless reset.
  • Static methods can be harder to substitute than an injected dependency.
  • Inheritance of static members and use of this can be useful but surprising; document whether subclasses share or shadow state.
  • Instance prototype methods are already shared rather than copied per object, so converting a method to static is not a general performance optimization.
  • API keys in browser code are visible regardless of whether they are stored in a static field, module constant, or minified bundle.

Further reading

Exercises

Check your understanding
Beta
Check your understanding Exercise
Check your understanding Exercise

Which uses are a good conceptual fit for static class members? Select all that apply.