JavaScript static methods are methods defined on a class itself rather than on individual instances of that class. You call a static method using the class name, without creating an object with new. Static methods are commonly used for utility functions, validation, calculations, and operations that belong to a class as a whole rather than to one particular object.
What Are Static Methods in JavaScript?
A static method is declared inside a class using the static keyword. It belongs to the class constructor itself, not to the class’s prototype or its instances. Therefore, you call it directly on the class name.
Static methods were introduced with JavaScript class syntax in ECMAScript 2015 (ES6). They are useful when a method does not need instance-specific data such as an object’s name, age, or other properties.
Basic Syntax of a JavaScript Static Method
To create a static method, write the static keyword before the method name in the class body.
class MathHelper {
static add(a, b) {
return a + b;
}
}
console.log(MathHelper.add(10, 20)); // 30
In this example, add() is a static method of the MathHelper class. It is called as MathHelper.add(). You do not need to create an instance of the class to use it.
Calling Static Methods Using the Class Name
Static methods are called through the class constructor. Creating an object does not make the static method available as an instance method.
class User {
static getMessage() {
return "Welcome to JavaScript.";
}
}
console.log(User.getMessage());
// "Welcome to JavaScript."
const user1 = new User();
// This is not a valid way to call a static method:
// console.log(user1.getMessage());
The method belongs to User, not to user1. If you try to call user1.getMessage(), JavaScript will throw a TypeError because the instance does not have that method in its property lookup chain.
Static Methods vs. Instance Methods
The main difference is where each method is available. An instance method is available on objects created from a class. A static method is available on the class itself.
class Person {
constructor(name) {
this.name = name;
}
// Instance method
greet() {
return "Hello, " + this.name;
}
// Static method
static category() {
return "Human";
}
}
const person1 = new Person("Nadia");
console.log(person1.greet()); // "Hello, Nadia"
console.log(Person.category()); // "Human"
The greet() method uses the individual object’s name property, so it is an instance method. The category() method returns information about the class as a whole, so it is static.
Using Static Methods for Utility Functions
Static methods are a good fit for utility operations that do not depend on a particular instance. For example, a class can provide reusable methods for converting units or performing calculations.
class Temperature {
static toCelsius(fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
static toFahrenheit(celsius) {
return (celsius * 9 / 5) + 32;
}
}
console.log(Temperature.toCelsius(98.6)); // 37
console.log(Temperature.toFahrenheit(0)); // 32
Both conversion methods work only with the values passed as arguments. They do not need data stored in a Temperature object, so defining them as static methods makes their purpose clear.
Using Static Methods with the this Keyword
Inside a static method, this normally refers to the class constructor on which the method was called. This is useful when one static method calls another static method or accesses a static property.
class Calculator {
static multiply(a, b) {
return a * b;
}
static square(number) {
return this.multiply(number, number);
}
}
console.log(Calculator.square(5)); // 25
When Calculator.square(5) runs, this refers to Calculator. Therefore, this.multiply() calls the static multiply() method on that class. As with other method calls, the value of this depends on how the method is called; extracting a static method into a standalone variable can change its this value.
Static Properties and Static Methods
A class can have static properties as well as static methods. Static properties store data on the class itself, and static methods can read or update that data through the class name or this.
class Counter {
static count = 0;
static increment() {
this.count++;
return this.count;
}
}
console.log(Counter.increment()); // 1
console.log(Counter.increment()); // 2
console.log(Counter.count); // 2
The count property belongs to the Counter class, not to each object created from it. Static class fields are supported in modern JavaScript environments. If you need to support older runtimes, check the environment’s class-field support or use an alternative initialization approach.
Static Methods and JavaScript Inheritance
Static methods can be inherited by a child class. When a class extends another class, the child constructor has the parent constructor in its prototype chain. This allows the child class to access inherited static methods.
class Animal {
static category() {
return "Animal";
}
}
class Dog extends Animal {}
console.log(Animal.category()); // "Animal"
console.log(Dog.category()); // "Animal"
The Dog class inherits access to the static category() method from Animal. The method is not copied onto every dog instance; it is available through the class inheritance chain.
Overriding Static Methods
A child class can define a static method with the same name as a static method in its parent class. The child implementation is used when the method is called on the child class. You can use super inside the child static method to call the parent’s version.
class Animal {
static sound() {
return "Animals make sounds.";
}
}
class Dog extends Animal {
static sound() {
return super.sound() + " Dogs bark.";
}
}
console.log(Animal.sound());
// "Animals make sounds."
console.log(Dog.sound());
// "Animals make sounds. Dogs bark."
Here, Dog.sound() overrides the inherited static method. The call to super.sound() runs the parent’s implementation, and the child method adds more information to the returned string.
Static Methods in Built-in JavaScript Objects
JavaScript already provides many static methods on built-in constructors. These methods are called on the constructor itself rather than on a value created from it.
// Array static method
console.log(Array.isArray([1, 2, 3])); // true
// Number static method
console.log(Number.isInteger(25)); // true
// Object static method
const user = { name: "Ava" };
console.log(Object.keys(user)); // ["name"]
// String static method
console.log(String.fromCharCode(65)); // "A"
For example, Array.isArray() checks whether a value is an array, while Object.keys() returns an array of an object’s own enumerable string-keyed property names. These operations do not need to be attached to a particular array or object instance.
Can You Call a Static Method from an Instance?
Not directly. Static methods are not placed on the class’s instance prototype, so an instance normally cannot access them through property lookup. You should call the method using the class name.
class Product {
static getCategory() {
return "Electronics";
}
}
const product1 = new Product();
console.log(Product.getCategory()); // "Electronics"
// product1.getCategory(); // TypeError
If an instance needs to use the same operation, you can call the static method through the class name from an instance method, or define a separate instance method that delegates to it.
class Product {
static getCategory() {
return "Electronics";
}
describe() {
return "Category: " + Product.getCategory();
}
}
const product1 = new Product();
console.log(product1.describe()); // "Category: Electronics"
When Should You Use Static Methods?
Use static methods when an operation logically belongs to a class but does not need the data of a particular instance. Typical examples include helper calculations, input validation, factory methods that create instances, and methods that return information about a class.
Do not make every method static. If a method needs to read or update instance properties through this, it should usually be an instance method. Choose the method type based on whether the behavior belongs to the class as a whole or to one object.
