Back to course

Static Methods

JavaScript: The Complete '0 to Hero' Beginner Course

75. Static Methods

Static methods are functions that belong to the class itself, rather than to any specific instance of the class. They are used for utility functions that don't rely on the state (this properties) of a particular object instance.

Defining Static Methods

Use the static keyword before the method name.

javascript class MathUtility { // Static property static PI = 3.14159;

// Static method
static calculateArea(radius) {
    // Cannot use 'this' here as it is not bound to an instance
    return this.PI * (radius ** 2);
}

// Instance method (requires 'new')
doSomething() { console.log('Instance method'); }

}

Calling Static Methods

They are called directly on the class name.

javascript // Called on the class, no instance needed const area = MathUtility.calculateArea(5); console.log(area); // 78.53975

// const utility = new MathUtility(); // utility.calculateArea(5); // ERROR

Use Case: Helper functions, factory methods, or constants that relate to the class structure globally.