Javatpoint Logo
Javatpoint Logo

JavaScript static Method

The JavaScript provides static methods that belong to the class instead of an instance of that class. So, an instance is not required to call the static method. These methods are called directly on the class itself.

Points to remember

  • The static keyword is used to declare a static method.
  • The static method can be of any name.
  • A class can contain more than one static method.
  • If we declare more than one static method with a similar name, the JavaScript always invokes the last one.
  • The static method can be used to create utility functions.
  • We can use this keyword to call a static method within another static method.
  • We cannot use this keyword directly to call a static method within the non-static method. In such case, we can call the static method either using the class name or as the property of the constructor.

JavaScript static Method Example 1

Let's see a simple example of a static method.

Test it Now

Output:

static method is invoked

Example 2

Le's see an example to invoke more than one static method.

Test it Now

Output:

static method is invoked
static method is invoked again

Example 3

Let's see an example to invoke more than one static method with similar names.

Test it Now

Output:

static method is invoked again

Example 4

Let's see an example to invoke a static method within the constructor.

Test it Now

Output:

static method is invoked
static method is invoked 

Example 5

Let's see an example to invoke a static method within the non-static method.

Test it Now

Output:

static method is invoked
Next TopicJS Encapsulation




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials