W3docs

JavaScript Static Class Members

Learn JavaScript static properties and methods: how to define them with the static keyword, inherit them in subclasses.

Static properties and methods belong to a class itself, not to instances created from it. That means you call them on the class name directly — without ever writing new. They are the right tool for utility functions and shared constants that concern the class as a whole rather than any single object.

If you have already used JavaScript's built-in objects, you have used static members without naming them as such:

  • Date.now() returns the current timestamp — you call it on Date, not on a date instance.
  • Array.from(iterable) builds an array — called on the Array class, not on an existing array.
  • Object.keys(obj) lists an object's own keys — called on Object.
  • Math.max(...) and Number.isInteger(...) are static too.

This chapter explains how to define your own static properties and methods, how they are inherited by subclasses, how this behaves inside them, and the common patterns (factory methods and static initialization blocks) you will reach for in real code. If classes are new to you, start with class basic syntax first.

Defining Static Properties and Methods

Static properties and methods are defined using the static keyword inside a class body. Here's how you define and use them:


javascript— editable

In this example, pi is a static property, and sum is a static method of the Calculator class. They can be accessed directly through the class name without instantiating it.

Note: The static class field syntax (static pi = ...) requires ES2022+ or a transpiler like Babel. Also, a static method cannot reach an instance's properties through this, because it belongs to the class itself rather than to any specific instance.

this Inside a Static Method

Inside a static method, this refers to the class, not to an instance. This lets one static method call another static method or read a static property on the same class:


javascript— editable

Because this resolves to the class, the same static method behaves differently depending on which class it is called through — a key detail once inheritance enters the picture (see the next section).

Inheriting Static Members in Subclasses

Static properties and methods are inherited. When a subclass extends a parent class, it gains access to the parent's static members and can call them through its own name:


javascript— editable

Here Advanced.sum is inherited from Calculator. Inside Advanced.double, this is Advanced, so this.sum(...) finds sum up the prototype chain.

Because this points at the class the method was called on, a subclass can override a static member and the parent's other static methods will use the overridden version when called through the subclass:


javascript— editable

For more on extending classes, see class inheritance.

Static Factory Methods

A common pattern is a factory method: a static method that builds and returns an instance of the class, often from data in a different shape. This keeps construction logic in one named place and makes the calling code read clearly.

A typical example is creating an object from a JSON string or a plain object received from an API:


javascript— editable

Using new this(...) instead of new User(...) means a subclass calling fromJSON automatically gets an instance of the subclass.

When to Use Static Properties

Static properties are ideal for constants related to a class that remain unchanged, regardless of class instance. They provide a way to access shared data from anywhere in your code. For example, if you have a class that deals with user interactions, you might have a static property that defines default settings or limits:


javascript— editable

For constants that should never be reassigned from outside the class, consider combining static fields with private members (static #secret = ...).

Static Initialization Blocks

Sometimes a static property needs more than a single expression to set up — for example, it depends on several other statics, requires a loop, or needs a try/catch. ES2022 added static initialization blocks (static { ... }) for exactly this. The block runs once, when the class is defined, with this bound to the class:


javascript— editable

You can have more than one static block, and they run in source order alongside static field initializers. They are ideal for computed setup that would be awkward to express as a single field assignment.

Advantages of Using Static Properties and Methods

  • Memory Efficiency: Since static properties and methods are tied to the class itself rather than instances, they avoid duplicating data or functions for each object, which can reduce memory overhead when many instances are created.
  • Convenience: Static methods can be called without instantiating the class, making them convenient for utility functions.
  • Organized Code: By grouping related functionality directly within classes as static members, code is kept organized and functional areas are clearly defined.

Summary

  • Static members are declared with the static keyword and live on the class, not on instances — call them as ClassName.member.
  • Familiar built-ins like Date.now(), Array.from(), and Object.keys() are static methods.
  • Inside a static method, this is the class it was called on. Subclasses inherit static members and can override them, and this resolves accordingly.
  • Use factory methods (static fromJSON(...) returning new this(...)) to centralize object construction, and static initialization blocks (static { ... }, ES2022) for setup that needs more than a single expression.
  • Reach for static members for utilities and shared constants; use regular (instance) methods for behavior that depends on a specific object's data.

Next steps

Practice

Practice
Which statements are true regarding static properties and methods in JavaScript?
Which statements are true regarding static properties and methods in JavaScript?
Was this page helpful?