Javascript - Class Static Methods

Introduction

With classes, you can define static properties and methods that are a part of a class and not particularly any instance.

It is useful in creating utility functions for an application.

The following code shows how to create static methods by using the static keyword before the method name:

class Bus { 
        static radio(message) { 
                console.log(`Message from broadcast: ${message}`) 
        } 
} 

Bus.radio("Hi"); 
// Message from broadcast: Hi 

The static methods and properties are accessed directly from their class.

You cannot access a static method or property from the instance of the class.

static methods cannot access the properties or methods defined on an instance of the class using this.