JavaScript Static Methods
Last Updated : 20 Jan, 2025
Static methods are functions that are defined on a class but are not accessible through instances of the class. Instead, they are called directly on the class itself. These methods are useful for creating utility functions or shared logic that doesn’t depend on individual object instances.
Syntax
class ClassName {
static methodName() {
// method logic
}
}
JavaScript class MathUtils { static add(a, b) { return a + b; } static multiply(a, b) { return a * b; } } // Calling static methods on the class console.log(MathUtils.add(5, 3)); console.log(MathUtils.multiply(4, 6));
In this example, the add and multiply methods can be called directly on the MathUtils class, without creating an instance.
Characteristics of Static Methods
- Class-Based: Static methods are invoked on the class itself, not on instances of the class.
- No Access to Instance Properties: Static methods do not have access to ‘this’ or instance-specific properties.
- Common Use Cases:
- Utility functions (e.g., mathematical operations, string manipulations).
- Factory methods for object creation.
- Shared logic that doesn’t depend on individual objects.
Use case of Static Methods
1. Static Method Math.max()
The getMax() method is a static method that wraps the built-in Math.max() function, demonstrating how static methods can be used to perform operations directly on the class.
JavaScript class Calc { static getMax(...numbers) { return Math.max(...numbers); } } console.log(Calc.getMax(1, 5, 3, 9));
The getMax() method takes any number of arguments and returns the highest value using the Math.max() function.
2. Static Method Array.isArray()
Static methods like checkArray() allow you to check for specific conditions (e.g., checking if a variable is an array) without needing to instantiate the class.
JavaScript class Validator { static check(input) { return Array.isArray(input); } } console.log(Validator.check([1, 2, 3])); console.log(Validator.check('hello'));
The checkArray() method uses the Array.isArray() method to determine if the input is an array.
3. Static Method Counter
Static methods can be used to manage class-level state, such as a counter, without creating individual instances.
JavaScript class Count { static c = 0; static inc() { return ++Count.c; } static reset() { Count.c = 0; } } console.log(Count.inc()); console.log(Count.inc()); Count.reset(); console.log(Count.inc());
The Count class has a static property c, which keeps track of the number of times the inc() method is called. The reset() method allows resetting the counter.
4. Static Method Factory Pattern
Static methods can serve as factory methods to create instances of a class, simplifying object creation and initialization.
JavaScript class User { constructor(name, age) { this.name = name; this.age = age; } static create(name, age) { return new User(name, age); } } const user = User.create('Ajay', 30); console.log(user);
The createUser() static method is a factory method that returns a new instance of the User class.
5. Static Method Singleton Pattern
Static methods can be used to implement design patterns like the Singleton, where only one instance of the class is allowed.
JavaScript class DB{ static instance constructor() { if(DB.instance) { return DB.instance } DB.instance=this } static getinstance() { if(!DB.instance) { DB.instance=new DB() } return DB.instance } } const obj1=new DB() const obj2=new DB() console.log(obj1===obj2)
In the DB class, the getInstance() method ensures that only one instance of the class is created. If an instance already exists, it returns the existing one. This is an implementation of the Singleton pattern, ensuring a single point of access to the class.
Similar Reads
JS Static Methods
Static methods are functions that are defined on a class but are not accessible through instances of the class. Instead, they are called directly on the class itself. These methods are useful for creating utility functions or shared logic that doesnât depend on individual object instances. Syntax cl
3 min read
How to create Static Variables in JavaScript ?
To create a static variable in JavaScript, you can use a closure or a function scope to encapsulate the variable within a function. This way, the variable maintains its state across multiple invocations of the function. Static keyword in JavaScript: The static keyword is used to define a static meth
2 min read
Static Function in PHP
In certain cases, it is very handy to access methods and properties in terms of a class rather than an object. This can be done with the help of static keyword. Any method declared as static is accessible without the creation of an object. Static functions are associated with the class, not an insta
3 min read
Comparison between static and instance method in PHP
Static methods The static method in PHP is same as other OOP languages. Static method should be used only when particular data remains constant for the whole class. As an example, consider that some programmer is making the data of a college and in that every object needs getCollegeName function tha
3 min read
When to use static vs instantiated classes in PHP?
Prerequisite - Static Function PHP In PHP, we can have both static as well as non-static (instantiated) classes. Static class Introduction: A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member (variable) or a static member function (m
5 min read
Static Method in Java With Examples
In Java, the static keyword is used to create methods that belongs to the class rather than any specific instance of the class. Any method that uses the static keyword is referred to as a static method. Features of Static Method: A static method in Java is associated with the class, not with any obj
3 min read
Static Method vs Instance Method in Java
In Java, methods are mainly divided into two parts based on how they are associated with a class, which are the static method and the Instance method. The main difference between static and instance methods is: Static method: This method belongs to the class and can be called without creating an obj
4 min read
Difference between static and non-static method in Java
A static method is a method that belongs to a class, but it does not belong to an instance of that class and this method can be called without the instance or object of that class. Every method in java defaults to a non-static method without static keyword preceding it. Non-static methods can access
6 min read
Static method in Interface in Java
Static Methods in Interface are those methods, which are defined in the interface with the keyword static. Unlike other methods in Interface, these static methods contain the complete definition of the function and since the definition is complete and the method is static, therefore these methods ca
2 min read
Static Variables in Java
In Java, when a variable is declared with the static keyword. Then, a single variable is created and shared among all the objects at the class level. Static variables are, essentially, global variables. All instances of the class share the same static variable. These are the main scenarios when we u
3 min read