Static Methods
Static methods belong to the class itself, not to instances of the class. You call them directly on the class name using the static keyword. They're useful for utility functions that are related to the class but don't need access to instance data.
class MathHelper {
static add(a, b) {
return a + b;
}
static multiply(a, b) {
return a * b;
}
}
// Called on the class, not on an instance
console.log(MathHelper.add(2, 3)); // 5
console.log(MathHelper.multiply(4, 5)); // 20
When to Use Static Methods
Use static methods for utility functions, factory methods, or operations that don't depend on a specific object's state. They're like helper functions that logically belong to the class.
class DateHelper {
static formatDate(date) {
return date.toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
});
}
static isToday(date) {
const today = new Date();
return date.toDateString() === today.toDateString();
}
}
DateHelper.formatDate(new Date()); // "July 15, 2026"
Static Factory Methods
A powerful pattern: using static methods as alternative constructors. They can return different objects based on input, which constructors alone can't do.
class User {
constructor(name, role) {
this.name = name;
this.role = role;
}
static createAdmin(name) {
return new User(name, "admin");
}
static createGuest() {
return new User("Guest", "guest");
}
}
const admin = User.createAdmin("Alice");
const guest = User.createGuest();
Try it Yourself →
Static Properties
You can also have static properties — values that belong to the class, not instances. These are useful for configuration or shared state.
class AppConfig {
static MAX_RETRIES = 3;
static API_URL = "https://api.example.com";
static shouldRetry(attempt) {
return attempt < AppConfig.MAX_RETRIES;
}
}
console.log(AppConfig.MAX_RETRIES); // 3
Summary
Static methods and properties belong to the class itself. Use them for utilities, factory methods, and class-level configuration that doesn't require an instance.