In JavaScript, Static Methods are functions defined on a class rather than on its instances. They are part of the class itself, which means you call them directly using the class name, not through an object created with the new keyword.
Static methods are typically used for “utility” functions—operations that relate to the class in general but don’t require data from a specific instance.
1. Syntax and Basic Example
You define a static method by prefixing the function name with the static keyword inside the class body.
JavaScript
class Calculator {
// Static method
static add(a, b) {
return a + b;
}
}
// ✅ Correct: Calling directly on the class
console.log(Calculator.add(5, 10)); // 15
// ❌ Incorrect: Calling on an instance
const myCalc = new Calculator();
console.log(myCalc.add(5, 10)); // TypeError: myCalc.add is not a function
2. When to Use Static Methods
Static methods are ideal for tasks where the logic is independent of the individual object’s state (this).
A. Utility Functions
If a function performs a task that is relevant to the class but doesn’t need to know about a specific object’s properties, make it static.
JavaScript
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
// Static method to calculate distance between two points
static distance(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.hypot(dx, dy);
}
}
const p1 = new Point(5, 5);
const p2 = new Point(10, 10);
console.log(Point.distance(p1, p2));
B. Factory Methods
Static methods are often used to create new instances of a class in a specific way, providing an alternative to the standard constructor.
JavaScript
class User {
constructor(name, role) {
this.name = name;
this.role = role;
}
// Factory method to create an Admin
static createAdmin(name) {
return new User(name, 'admin');
}
}
const admin = User.createAdmin("Sarah");
3. The this Keyword in Static Methods
Inside a static method, the this keyword refers to the class itself, not an instance of the class. This allows static methods to call other static methods or access static properties.
JavaScript
class Logger {
static prefix = "[LOG]";
static format(message) {
return `${this.prefix}: ${message}`; // 'this' is the Logger class
}
static log(message) {
console.log(this.format(message));
}
}
4. Static Inheritance
Unlike some other languages, JavaScript supports the inheritance of static methods. When one class extends another, the child class also receives the parent’s static methods.
JavaScript
class Parent {
static greet() {
return "Hello from Parent";
}
}
class Child extends Parent {}
console.log(Child.greet()); // "Hello from Parent"
5. Real-World Example: Database Models
In many backend frameworks (like Sequelize or Mongoose), static methods are used to perform operations on the collection (like searching for a user), while instance methods are used for operations on a specific row (like updating a password).
JavaScript
class User {
// Static: Operates on the "all users" concept
static findByEmail(email) {
// Database logic: SELECT * FROM users WHERE email = ...
}
// Instance: Operates on this specific user
save() {
// Database logic: UPDATE users SET ... WHERE id = this.id
}
}
