static

The static keyword is used in a class to define methods and properties that are associated with the class itself, rather than with instances of the class. This means you can call static methods and access static properties on the class itself, without needing to create an instance of the class. Static members are often used for utility functions or properties that are shared among all instances of the class.

Anything that is static belongs to the class, not the objects.


class Car {
  static numberOfCars = 0;
  constructor(model) {
    this.model = model;
    Car.numberOfCars += 1;
  }
  static startRace() {
    console.log(`GO!`);
  }
}

const car1 = new Car("Fiat");
const car2 = new Car("Porsche");
const car3 = new Car("Ferrari");

console.log(Car.numberOfCars);
console.log(Car.startRace());