SyntaxStudy
Sign Up
JavaScript Static Methods and Properties
JavaScript Intermediate 7 min read

Static Methods and Properties

Static Methods and Properties

Static class members belong to the class itself rather than to any individual instance. They are accessed on the class constructor (e.g., MyClass.staticMethod()), not on instances. Static members are ideal for utility functions, factory methods, constants, and shared configuration.

Static Methods

Prefix a method with the static keyword. Static methods cannot access this as an instance — this inside a static method refers to the class itself. They are not accessible on instances.

Static Properties (Fields)

Static fields (class fields proposal, widely supported) define shared properties on the class. They are set once when the class is evaluated, not per instance.

Static Inheritance

Subclasses inherit static methods from their parent class. You can override static methods just like instance methods.

Example
class MathHelper {
  static PI = 3.14159;
  static count = 0;
  constructor(value) {
    this.value = value;
    MathHelper.count++;
  }
  static add(a, b) { return a + b; }
  static multiply(a, b) { return a * b; }
  static circleArea(r) {
    return MathHelper.PI * r * r;
  }
}
console.log(MathHelper.add(2, 3));       // 5
console.log(MathHelper.multiply(4, 5));  // 20
console.log(MathHelper.circleArea(5));   // 78.53975
new MathHelper(10);
new MathHelper(20);
console.log(MathHelper.count);          // 2
// console.log(new MathHelper(1).add(2,3)); // TypeError
Pro Tip

Use static factory methods (e.g., User.fromJSON(data)) to provide alternative constructors — they can contain validation logic, transform input, and return an instance, acting as named constructors.