JavaScript Class Static Methods
Subject: JavaScript
In JavaScript, static methods are defined on the class itself, not on instances. They are useful for utility or helper functions that don't require instance properties. Static methods are declared with the static keyword and called using the class name only.
Syntax
Call a static method:
Example 1: Creating a Static Method
add()is called on the class, not on instances.
Example 2: Static Methods Are Not Inherited by Instances
Example 3: Use Case for Utility Methods
Useful when logic doesn’t depend on object state.
Example 4: Static Method in Inheritance
Static methods are inherited by subclasses and callable on them.
Key Takeaways
- Static methods belong to the class, not instances.
- Declared using the
statickeyword. - Ideal for utility functions or shared logic.
- Cannot be called on instances.
- Are inherited by subclasses.