JavaScript Class Static Methods and Properties
Static methods and properties belong to the class itself, not to instances of the class. They are called directly on the class and cannot be called on individual objects created from the class.
Static Methods
A static method is defined using the static keyword. Static methods are often used for utility functions that don't require object-specific data.
class MathHelper {
static add(a, b) {
return a + b;
}
static multiply(a, b) {
return a * b;
}
}
// Call on the class, not an instance
console.log(MathHelper.add(5, 3));
console.log(MathHelper.multiply(4, 7));
// This would throw an error:
// const helper = new MathHelper();
// helper.add(5, 3); // TypeErrorStatic Properties
Static properties are class-level variables that are shared among all instances. They are useful for storing data that is common to all objects of a class, such as counters or configuration.
class User {
static count = 0;
constructor(name) {
this.name = name;
User.count++;
}
static getCount() {
return 'Total users: ' + User.count;
}
}
const u1 = new User('Alice');
const u2 = new User('Bob');
const u3 = new User('Charlie');
console.log(User.getCount());
console.log(User.count);When to Use Static
Use static methods when the logic does not depend on instance data. Common use cases include factory methods, utility/helper functions, caching, and configuration.
class Product {
constructor(name, price) {
this.name = name;
this.price = price;
}
static fromJSON(json) {
const data = JSON.parse(json);
return new Product(data.name, data.price);
}
describe() {
return this.name + ': $' + this.price;
}
}
const json = '{"name": "Laptop", "price": 999}';
const laptop = Product.fromJSON(json);
console.log(laptop.describe());
console.log(laptop instanceof Product);Math as an Example of Static Methods
The built-in Math object is a great example of static methods in JavaScript. All Math methods are called directly on Math, not on an instance.
// Math methods are all static
console.log(Math.round(4.7));
console.log(Math.floor(4.9));
console.log(Math.ceil(4.1));
console.log(Math.max(10, 20, 30));
console.log(Math.min(10, 20, 30));
console.log(Math.sqrt(64));
// You cannot create an instance of Math
// const m = new Math(); // TypeErrorStatic vs Instance and Private Fields
Understanding the difference between static and instance members is crucial. Additionally, JavaScript supports private fields using the # prefix, which restricts access to within the class.
| Feature | Static | Instance |
|---|---|---|
| Belongs to | The class itself | Each object |
| Access | ClassName.method() | object.method() |
| 'this' refers to | The class | The instance |
| Inherited | Yes (by child class) | Yes (by child instances) |
| Use case | Utility functions, factories | Object-specific behavior |
class BankAccount {
#balance = 0;
constructor(owner, initialBalance) {
this.owner = owner;
this.#balance = initialBalance;
}
deposit(amount) {
this.#balance += amount;
return this.#balance;
}
getBalance() {
return this.#balance;
}
}
const account = new BankAccount('Alice', 1000);
console.log(account.owner);
console.log(account.getBalance());
account.deposit(500);
console.log(account.getBalance());
// console.log(account.#balance); // SyntaxError: Private field