JavaScript Class Static Methods and Properties

Static methods and properties belong to the class itself, not to instances of the class. They are called directly on the class and cannot be called on individual objects created from the class.

Static Methods

A static method is defined using the static keyword. Static methods are often used for utility functions that don't require object-specific data.

Defining Static Methods
class MathHelper {
  static add(a, b) {
    return a + b;
  }

  static multiply(a, b) {
    return a * b;
  }
}

// Call on the class, not an instance
console.log(MathHelper.add(5, 3));
console.log(MathHelper.multiply(4, 7));

// This would throw an error:
// const helper = new MathHelper();
// helper.add(5, 3); // TypeError

Static Properties

Static properties are class-level variables that are shared among all instances. They are useful for storing data that is common to all objects of a class, such as counters or configuration.

Static Properties
class User {
  static count = 0;

  constructor(name) {
    this.name = name;
    User.count++;
  }

  static getCount() {
    return 'Total users: ' + User.count;
  }
}

const u1 = new User('Alice');
const u2 = new User('Bob');
const u3 = new User('Charlie');

console.log(User.getCount());
console.log(User.count);

When to Use Static

Use static methods when the logic does not depend on instance data. Common use cases include factory methods, utility/helper functions, caching, and configuration.

Factory Method Pattern
class Product {
  constructor(name, price) {
    this.name = name;
    this.price = price;
  }

  static fromJSON(json) {
    const data = JSON.parse(json);
    return new Product(data.name, data.price);
  }

  describe() {
    return this.name + ': $' + this.price;
  }
}

const json = '{"name": "Laptop", "price": 999}';
const laptop = Product.fromJSON(json);
console.log(laptop.describe());
console.log(laptop instanceof Product);

Math as an Example of Static Methods

The built-in Math object is a great example of static methods in JavaScript. All Math methods are called directly on Math, not on an instance.

Built-in Static Methods (Math)
// Math methods are all static
console.log(Math.round(4.7));
console.log(Math.floor(4.9));
console.log(Math.ceil(4.1));
console.log(Math.max(10, 20, 30));
console.log(Math.min(10, 20, 30));
console.log(Math.sqrt(64));

// You cannot create an instance of Math
// const m = new Math(); // TypeError

Static vs Instance and Private Fields

Understanding the difference between static and instance members is crucial. Additionally, JavaScript supports private fields using the # prefix, which restricts access to within the class.

FeatureStaticInstance
Belongs toThe class itselfEach object
AccessClassName.method()object.method()
'this' refers toThe classThe instance
InheritedYes (by child class)Yes (by child instances)
Use caseUtility functions, factoriesObject-specific behavior
Private Fields with #
class BankAccount {
  #balance = 0;

  constructor(owner, initialBalance) {
    this.owner = owner;
    this.#balance = initialBalance;
  }

  deposit(amount) {
    this.#balance += amount;
    return this.#balance;
  }

  getBalance() {
    return this.#balance;
  }
}

const account = new BankAccount('Alice', 1000);
console.log(account.owner);
console.log(account.getBalance());
account.deposit(500);
console.log(account.getBalance());
// console.log(account.#balance); // SyntaxError: Private field
📝 Note: Static methods cannot access instance properties using 'this'. Inside a static method, 'this' refers to the class itself, not an instance. Private fields (#) are truly private and cannot be accessed from outside the class, unlike the old convention of prefixing with an underscore (_).
Exercise:
How do you call a static method named 'create' on a class named 'Item'?
Try it YourselfCtrl+Enter to run
Click Run to see the output here.