What Are Static Methods?
You'll learn what static methods are and when they're useful for class-level operations.
The Core Idea
A static method is a function defined on a class that belongs to the class itself, not to individual instances. You call static methods directly on the class constructor, and they don't have access to instance-specific data.
Think of a static method like a general utility that's related to the class concept but doesn't need to work with any particular object. For example, a MathHelper class might have a static method MathHelper.add(a, b) — it does math, but it doesn't need a specific MathHelper instance to do its job.
Defining Static Methods
Use the static keyword before a method name inside a class:
class Calculator {
static add(a, b) {
return a + b;
}
}
// Call it on the class itself, not an instance
console.log(Calculator.add(5, 3)); // 8
// This WON'T work:
const calc = new Calculator();
// calc.add(5, 3); // Error: calc.add is not a function
When to Use Static Methods
Static methods shine when you need functionality that's related to the class concept but doesn't depend on instance data. Common use cases include factory methods that create instances, utility functions, or validation helpers.
Key Takeaway: Static methods belong to the class constructor itself and are called without creating an instance, making them perfect for operations that don't need instance-specific data.