Methods That Belong to the Class Itself

Static methods and properties belong to the class itself, not to its instances. They are called directly on the class name.

class Article {
  constructor(title, date) {
    this.title = title;
    this.date = date;
  }

  // Called on the CLASS, not on an instance
  static compare(a, b) {
    return a.date - b.date;
  }

  static createDefault() {
    return new Article('Untitled', new Date());
  }
}

const articles = [new Article('B', new Date(2024, 0, 1)), new Article('A', new Date(2023, 0, 1))];
articles.sort(Article.compare); // Sort by date

const draft = Article.createDefault(); // Factory method

When to Use Static

  • Utility functions: operations that don't need instance data (e.g., comparison, parsing)
  • Factory methods: alternative ways to create instances
  • Shared constants: configuration values related to the class
Note

Static properties and methods are inherited! If a parent class has a static method, it's accessible through child class names too.