You will be able to define and call JavaScript static methods, use them for class-level utilities and factory methods, and avoid confusing them with instance methods.
What it is
A static method is a function attached to a class itself, not to objects created from that class. You call it with the class name, such as User.create(), instead of calling it on an instance like user.create(). Static methods are useful when behavior belongs to the concept of the class rather than to one particular object.
Related terms: instance method is available on objects created by the class, constructor is the special method called with new, factory method is a static method that creates and returns an instance, and static property is a value stored on the class.
Why it matters
- It lets a class provide helper functions without creating an object first.
- It supports factory methods, such as
User.fromForm(), that centralize object creation. - It keeps class-related logic organized inside the class instead of scattering it across global functions.
- It can validate input before an instance is created.
- It can expose constants or utility behavior that applies to all instances.
Syntax or steps
class Name {
static methodName(argument) {
return ...;
}
}
Name.methodName(value);
The static keyword places the method on the class. Inside a static method, this refers to the class itself, not to an instance. Therefore, a static method should not try to read instance properties such as this.name unless it is working with an object it created or received.
Example
class User {
constructor(name, email) {
this.name = name;
this.email = email;
}
static fromForm(form) {
return new this(form.name, form.email);
}
static isValidEmail(email) {
return email.includes("@");
}
}
const user = User.fromForm({ name: "Ada", email: "ada@example.com" });
console.log(user.name);
console.log(user instanceof User);
console.log(User.isValidEmail("bad-email"));
constructor(name, email)creates a normal instance and stores instance data.static fromForm(form)is a factory method. It receives plain data and returns a newUser.new this(...)creates an instance of the class that called the method. If a subclass inherits this static method, it can create the subclass instead.static isValidEmail(email)is a utility method. It checks data without needing an existing user object.User.fromForm(...)andUser.isValidEmail(...)are called on the class, not on an instance.
Common mistakes
- Calling a static method on an instance, such as
user.fromForm(). Fix: call it on the class, such asUser.fromForm(). - Using
this.nameinside a static method and expecting an instance property. Fix: use a parameter, or create an instance first. - Forgetting the
statickeyword. Fix: addstaticif the method belongs to the class itself. - Using static methods for behavior that depends on individual object state. Fix: use an instance method when the behavior needs
thisas the current object.
When to use it
| Choice | Use when | Example |
|---|---|---|
| Static method | The behavior belongs to the class, such as creating instances or validating input. | User.fromForm() |
| Instance method | The behavior depends on one object’s data. | user.rename() |
| Module function | The helper is not strongly tied to one class. | formatDate() |
Practice
Guided exercise: create a Temperature class with a constructor that stores Celsius and a static method that converts Celsius to Fahrenheit.
class Temperature {
constructor(celsius) {
this.celsius = celsius;
}
static cToF(celsius) {
return celsius * 9 / 5 + 32;
}
}
console.log(Temperature.cToF(0));
Expected output: 32.
Challenge: add a static method called createGuest() that returns a new Guest with the name "Anonymous". Hint: use return new this("Anonymous").
Quick check
Question: What does this refer to inside a static method?
Answer: It refers to the class itself, not to an instance created by that class.
Summary
Static methods are class-level functions used for utilities, validation, and factory behavior. They are called on the class, not on instances, and they should not depend on instance state. Use them when the action belongs to the class as a whole.