Static Methods
Static methods belong to the class itself, not to instances.
class MathUtils {
static add(a, b) {
return a + b;
}
}
console.log(MathUtils.add(2, 3)); // 5You call a static method on the class:
MathUtils.add(2, 3);not on an instance.
Instance Method vs Static Method
Instance method:
class User {
constructor(name) {
this.name = name;
}
greet() {
return `Hello, ${this.name}`;
}
}
const user = new User("Alice");
console.log(user.greet()); // Hello, AliceStatic method:
class User {
static createGuest() {
return new User("Guest");
}
constructor(name) {
this.name = name;
}
}
const guest = User.createGuest();
console.log(guest.name); // GuestStatic methods are useful for behavior related to the class, but not to one specific instance.
The static Keyword
Use the static keyword before the method name.
class ClassName {
static methodName() {
// class-level behavior
}
}Example:
class Validator {
static isEmail(value) {
return value.includes("@");
}
}
console.log(Validator.isEmail("a@example.com")); // trueYou do not need to create a Validator instance.
Static Methods Cannot Access Instance Data Directly
Static methods are called on the class, so this does not refer to a normal instance.
class User {
constructor(name) {
this.name = name;
}
static describe() {
return this.name;
}
}
console.log(User.describe()); // UserInside a static method, this usually refers to the class itself.
It does not refer to an instance like new User("Alice").
If a method needs instance data, make it an instance method.
Static Factory Methods
Static methods are often used as factory methods.
A factory method creates an instance.
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();
console.log(admin.role); // admin
console.log(guest.role); // guestThis can make object creation more expressive.
Static Utility Methods
Static methods can group related helper functions.
class StringUtils {
static capitalize(value) {
return value.charAt(0).toUpperCase() + value.slice(1);
}
static slugify(value) {
return value.toLowerCase().replaceAll(" ", "-");
}
}
console.log(StringUtils.capitalize("javascript")); // Javascript
console.log(StringUtils.slugify("JavaScript Basics")); // javascript-basicsFor simple utilities, plain exported functions can also be a good choice.
export function capitalize(value) {}
export function slugify(value) {}Static methods are not always necessary.
Static Properties
Modern JavaScript also supports static fields.
class User {
static defaultRole = "user";
constructor(name, role = User.defaultRole) {
this.name = name;
this.role = role;
}
}
const user = new User("Alice");
console.log(user.role); // userStatic fields belong to the class.
console.log(User.defaultRole); // userInherited Static Methods
Static methods can be inherited by subclasses.
class User {
static describeType() {
return "User-like object";
}
}
class Admin extends User {}
console.log(Admin.describeType()); // User-like objectUse this carefully.
Sometimes inherited static behavior is useful, and sometimes it makes class relationships harder to understand.
Best Practices
Use static methods for class-level behavior.
Use instance methods for behavior that depends on instance data.
Use static factory methods when they make creation clearer.
Consider plain exported functions for general utilities.
Do not create an instance just to call a method that does not use instance data.
Common Mistakes
Mistake 1: Calling a Static Method on an Instance
class MathUtils {
static add(a, b) {
return a + b;
}
}
const utils = new MathUtils();
utils.add(2, 3); // TypeErrorCorrect:
MathUtils.add(2, 3);Mistake 2: Expecting Static Methods to Read Instance Properties
class User {
constructor(name) {
this.name = name;
}
static greet() {
return `Hello, ${this.name}`;
}
}this in a static method is not an instance.
Use an instance method if you need instance data.
Mistake 3: Using Static Classes for Every Utility
This is not always needed:
class Formatters {
static formatDate(date) {}
}This may be simpler:
export function formatDate(date) {}Quick Check
What does this log?
class User {
static createGuest() {
return new User("Guest");
}
constructor(name) {
this.name = name;
}
}
const guest = User.createGuest();
console.log(guest.name);It logs:
GuestSummary
Static methods belong to the class, not instances.
- Use
staticbefore a method name. - Call static methods on the class.
- Static methods are useful for factories and class-level utilities.
- Instance methods are used when behavior depends on instance data.
- Static fields store class-level values.
- Static methods can be inherited.
- Plain exported functions are often simpler for general utilities.