JavaScript classes allow methods to be defined as static using the static keyword. A static method belongs to the class itself rather than to the objects created from that class.
Unlike regular methods, static methods cannot be called using an object instance. Instead, they must be invoked directly using the class name.
In this chapter, you will learn about JavaScript static methods, their syntax, features, and practical uses.
A static method is a method that belongs to the class itself instead of an instance of the class. Since static methods are associated with the class, they can be called directly using the class name without creating an object.
Static methods are commonly used for utility functions, helper methods, validation functions, and factory methods.
A static method is declared inside a class using the static keyword.
class ClassName {
static methodName() {
// code
}
}
Static methods are attached directly to the class and are not available on object instances. When a static method is called, JavaScript executes it in the context of the class rather than a specific object.
Because static methods do not belong to instances, they cannot directly access instance properties and methods.
The following examples demonstrate different ways to use static methods in JavaScript classes.
This example demonstrates how a static method can operate on multiple objects without belonging to any specific object. The static method compares the properties of two objects and displays the result.
JavaScript
Execute NowOutput:

This example demonstrates how one static method can invoke another static method using the this keyword. Since this refers to the class inside a static method, it can be used to access other static properties and methods.
JavaScript
Execute NowOutput:

This example demonstrates how a static method can be used as a factory method to create and return objects. Factory methods are commonly used to create class instances from JSON data, configuration objects, or other input formats.
JavaScript
Execute NowOutput:

A static method belongs to the class and is called using the class name, whereas an instance method belongs to an object and is called using an instance of the class. Static methods are typically used for utility operations, while instance methods operate on the data of individual objects.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India