JavaScriptMedium
Why might you want to create static class members in JavaScript?
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
Static class members (properties/methods) have a static keyword prepended. Such members cannot be directly accessed on instances of the class. Instead, they're accessed on the class itself.
js
class Car {
static noOfWheels = 4;
static compare() {
return "Static method has been called.";
}
}
console.log(Car.noOfWheels); // 4
Static members are useful under the following scenarios:
- Namespace organization: Static properties can be used to define constants or configuration values that are specific to a class. This helps organize related data within the class namespace and prevents naming conflicts with other variables. Examples include
Math.PI,Math.SQRT2. - Helper functions: Static methods can be used as helper functions that operate on the class itself or its instances. This can improve code readability and maintainability by separating utility logic from the core functionality of the class. Examples of frequently used static methods include
Object.assign(),Math.max(). - Singleton pattern: In some rare cases, static properties and methods can be used to implement a singleton pattern, where only one instance of a class ever exists. However, this pattern can be tricky to manage and is generally discouraged in favor of more modern dependency injection techniques.
Read the detailed answer on GreatFrontEnd which allows progress tracking, contains more code samples, and useful resources.
