DEV Community

Shanoy Sinclair
Shanoy Sinclair

Posted on • Updated on

Understanding what Static does in Javascript

I am sure you have probably watched a tutorial of Javascript where the instructor have used the static method on a class and not fully explain what static even does. Here is a simple explanation of what the functionalities are as stated by the Mozilla Developer Network.

The static keyword defines a static method for a class. Static methods aren't called on instances of the class. Instead, they're called on the class itself. These are often utility functions, such as functions to create or clone objects eg.

class Greet {
    static sayHi() {
        console.log("hello");
    }
}
Greet.sayHi(); // return 'hello'
Enter fullscreen mode Exit fullscreen mode

If we were to create an instance and call the method sayHi on that instance we would get a TypeError of not a function eg.

const greeting = new Greet();
greeting.sayHi(); // return TypeError: greeting.sayHi is not a function
Enter fullscreen mode Exit fullscreen mode

For more information on Static methods: click here

Top comments (0)