The constructor Method
Static methods and properties are defined on the class itself, not on instances. They are called using the class name. Static members are useful for utility functions, constants, or singleton-like behavior.
Key Points
Section titled “Key Points”- Declared with the
statickeyword. - Cannot be called on instances; only on the class.
- Can be inherited by subclasses.
- Static fields (ES2022) allow defining static properties directly.
Code Example
Section titled “Code Example”class MathUtils { static PI = 3.14159; // static field static add(a, b) { // static method return a + b; } static multiply(a, b) { return a * b; }}
console.log(MathUtils.PI); // 3.14159console.log(MathUtils.add(5, 3)); // 8
const utils = new MathUtils();// utils.add(2,3); // TypeError: utils.add is not a function