Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

static — static class elements

`static` defines fields, methods, and accessors belonging to the class itself rather than its instances. A `static { ... }` block performs initialization when the class is created, without calling `new`.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

class Name {
  static field = value;
  static method() { /* ... */ }
  static get property() { /* ... */ }
  static set property(value) { /* ... */ }
  static { /* initialization */ }
}

static before a field, method, or accessor (get or set) defines an element on the class constructor object, not its prototype or instances. Use Counter.next(), not new Counter().next(). static does not mean constant: a writable static field can change. Static elements can also be private, with a name starting with #.

Static field initializers and static { ... } blocks execute in source order while the class definition is evaluated, not on every new. Inside such an initializer or block, this refers to the class being defined. A block has its own scope and cannot directly use await or yield.

Public static elements are inherited by subclasses through the constructors' prototype chain. In a method called as Child.method(), this is Child. Private static elements are not installed on subclasses; an inherited method reading this.#field can therefore throw TypeError.

static is forbidden as a variable identifier in strict mode, but can be a property name. In a class, static() {} declares an instance method named static, not a static method. Static methods date from ECMAScript 2015; static fields and initialization blocks date from ECMAScript 2022. Support in a particular browser requires separate checking; the Node version used in a repository does not establish it.

Examples

class Counter {
  static value = 2;

  static {
    this.value *= 3;
  }

  static next() {
    return ++this.value;
  }
}

console.log(Counter.value);
console.log(Counter.next());
console.log(typeof new Counter().next);

Result

6
7
undefined

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources