Course contentsShow
← JavaScript
Lesson 785 of 2,72318. ClassesFree lesson

Static Properties

Classes can have static properties that are shared across all instances and accessed via the class name.

What You'll Learn

You'll learn how to define properties that belong to the class itself rather than to individual instances, and when this approach is useful.

Static Properties: Shared Data on the Class

Just as static methods belong to the class rather than instances, static properties are variables stored on the class itself. Unlike instance properties (which every object gets its own copy of), static properties are shared—there's only one copy, attached to the class.

Think of a class like a factory. Instance properties are characteristics of each product the factory makes (each product has its own serial number). Static properties are characteristics of the factory itself (like the factory's address—there's only one, and it doesn't belong to any single product).

Declaring Static Properties

Use the static keyword before a property name inside the class body:

class Counter {
  static count = 0; // static property

  constructor() {
    Counter.count++; // access via class name
  }
}

console.log(Counter.count); // 0

const c1 = new Counter();
console.log(Counter.count); // 1

const c2 = new Counter();
console.log(Counter.count); // 2

console.log(c1.count); // undefined (not on instances!)

Key Characteristics

  • Accessed via the class name, not instance references
  • Shared across all instances—there's only one copy
  • Cannot be accessed with this in instance methods (use the class name instead)
  • Common uses: configuration values, counters, caches, default values

Example: Configuration

class Database {
  static maxConnections = 100;
  static timeout = 5000;
}

console.log(Database.maxConnections); // 100

Key Takeaway: Static properties store data on the class itself, shared by all instances, and are accessed using the class name rather than instance references.