static
BaselineWidely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2016.
* Some parts of this feature may have varying levels of support.
Thestatic
keyword defines astatic method or field for a class, or astatic initialization block (see the link for more information about this usage). Static properties cannot be directly accessed on instances of the class. Instead, they're accessed on the class itself.
Static methods are often utility functions, such as functions to create or clone objects, whereas static properties are useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.
Note:In the context of classes, MDN Web Docs content uses the terms properties andfields interchangeably.
Try it
class ClassWithStaticMethod { static staticProperty = "someValue"; static staticMethod() { return "static method has been called."; } static { console.log("Class static initialization block called"); }}console.log(ClassWithStaticMethod.staticProperty);// Expected output: "someValue"console.log(ClassWithStaticMethod.staticMethod());// Expected output: "static method has been called."
Syntax
class ClassWithStatic { static staticField; static staticFieldWithInitializer = value; static staticMethod() { // … }}
There are some additional syntax restrictions:
- The name of a static property (field or method) cannot be
prototype
. - The name of a class field (static or instance) cannot be
constructor
.
Description
This page introduces public static properties of classes, which include static methods, static accessors, and static fields.
- For private static features, seeprivate elements.
- For instance features, seemethods definitions,getter,setter, andpublic class fields.
Public static features are declared using thestatic
keyword. They are added to the class constructor at the time ofclass evaluation using the[[DefineOwnProperty]]
semantic (which is essentiallyObject.defineProperty()
). They are accessed again from the class constructor.
Static methods are often utility functions, such as functions to create or clone instances. Public static fields are useful when you want a field to exist only once per class, not on every class instance you create. This is useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.
Static field names can becomputed. Thethis
value in the computed expression is thethis
surrounding the class definition, and referring to the class's name is aReferenceError
because the class is not initialized yet.await
andyield
work as expected in this expression.
Static fields can have an initializer. Static fields without initializers are initialized toundefined
. Public static fields are not reinitialized on subclasses, but can be accessed via the prototype chain.
class ClassWithStaticField { static staticField; static staticFieldWithInitializer = "static field";}class SubclassWithStaticField extends ClassWithStaticField { static subStaticField = "subclass field";}console.log(Object.hasOwn(ClassWithStaticField, "staticField")); // trueconsole.log(ClassWithStaticField.staticField); // undefinedconsole.log(ClassWithStaticField.staticFieldWithInitializer); // "static field"console.log(SubclassWithStaticField.staticFieldWithInitializer); // "static field"console.log(SubclassWithStaticField.subStaticField); // "subclass field"
In the field initializer,this
refers to the current class (which you can also access through its name), andsuper
refers to the base class constructor.
class ClassWithStaticField { static baseStaticField = "base static field"; static anotherBaseStaticField = this.baseStaticField; static baseStaticMethod() { return "base static method output"; }}class SubClassWithStaticField extends ClassWithStaticField { static subStaticField = super.baseStaticMethod();}console.log(ClassWithStaticField.anotherBaseStaticField); // "base static field"console.log(SubClassWithStaticField.subStaticField); // "base static method output"
The expression is evaluated synchronously. You cannot useawait
oryield
in the initializer expression. (Think of the initializer expression as being implicitly wrapped in a function.)
Static field initializers andstatic initialization blocks are evaluated one-by-one. Field initializers can refer to field values above it, but not below it. All static methods are added beforehand and can be accessed, although calling them may not behave as expected if they refer to fields below the one being initialized.
Note:This is more important withprivate static fields, because accessing a non-initialized private field throws aTypeError
, even if the private field is declared below. (If the private field is not declared, it would be an earlySyntaxError
.)
Examples
Using static members in classes
The following example demonstrates several things:
- How a static member (method or property) is defined on a class.
- That a class with a static member can be sub-classed.
- How a static member can and cannot be called.
class Triple { static customName = "Tripler"; static description = "I triple any number you provide"; static calculate(n = 1) { return n * 3; }}class SquaredTriple extends Triple { static longDescription; static description = "I square the triple of any number you provide"; static calculate(n) { return super.calculate(n) * super.calculate(n); }}console.log(Triple.description); // 'I triple any number you provide'console.log(Triple.calculate()); // 3console.log(Triple.calculate(6)); // 18const tp = new Triple();console.log(SquaredTriple.calculate(3)); // 81 (not affected by parent's instantiation)console.log(SquaredTriple.description); // 'I square the triple of any number you provide'console.log(SquaredTriple.longDescription); // undefinedconsole.log(SquaredTriple.customName); // 'Tripler'// This throws because calculate() is a static member, not an instance member.console.log(tp.calculate()); // 'tp.calculate is not a function'
Calling static members from another static method
In order to call a static method or property within another static method of the same class, you can use thethis
keyword.
class StaticMethodCall { static staticProperty = "static property"; static staticMethod() { return `Static method and ${this.staticProperty} has been called`; } static anotherStaticMethod() { return `${this.staticMethod()} from another static method`; }}StaticMethodCall.staticMethod();// 'Static method and static property has been called'StaticMethodCall.anotherStaticMethod();// 'Static method and static property has been called from another static method'
Calling static members from a class constructor and other methods
Static members are not directly accessible using thethis
keyword fromnon-static methods. You need to call them using the class name:CLASSNAME.STATIC_METHOD_NAME()
/CLASSNAME.STATIC_PROPERTY_NAME
or by calling the method as a property oftheconstructor
:this.constructor.STATIC_METHOD_NAME()
/this.constructor.STATIC_PROPERTY_NAME
class StaticMethodCall { constructor() { console.log(StaticMethodCall.staticProperty); // 'static property' console.log(this.constructor.staticProperty); // 'static property' console.log(StaticMethodCall.staticMethod()); // 'static method has been called.' console.log(this.constructor.staticMethod()); // 'static method has been called.' } static staticProperty = "static property"; static staticMethod() { return "static method has been called."; }}
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-class-definitions |