Skip to content
Related Articles

Related Articles

Improve Article

Static Methods in JavaScript

  • Difficulty Level : Hard
  • Last Updated : 19 Aug, 2020

The JavaScript allows static methods that belong to the class rather than an instance of that class. Hence, an instance is not needed to call such static methods. Static methods are called on the class directly. It can be of any name. A class can contain more than one static method. If we define more than one static method with the same name, the last method is invoked by JavaScript. “this” keyword is used to call a static method within any other static method in JavaScript.

Following are the different examples of JavaScript that demonstrates how to use and invoke static methods.

Example 1: The following is the code to invoke one static method. The script part of the code holds the JavaScript code.

HTML




<!DOCTYPE html>
<html>
  
<body>
  
    <script>
        class GeeksforGeeks { 
              
            // static keyword is used 
            // before the function name
            static example1() { 
                  
                // return the string -->
                // static method 1
                return "static method 1"
            }
        }
  
        // Calls the static function 
        // using className.functionName
        document.writeln(
            GeeksforGeeks.example1());
    </script>
</body>
  
</html>


Output:
static method 1

Example 2: The following code demonstrates how to invoke more than one static method.



HTML




<!DOCTYPE html>
<html>
  
<body>
    <script>
        class GeeksforGeeks {
  
            // static keyword is used 
            // before the function name
            static example1() {
  
                // return the string -->
                // static method 1
                return "static method 1"
            }
  
            // Another static method 
            // with different name 
            static example2() {
  
                // return the string -->
                // static method 2
                return "static method 2"
            }
  
        }
  
        // Calls the static function using
        // className.functionName
        document.writeln(GeeksforGeeks.example1() + "<br>");
        document.writeln(GeeksforGeeks.example2());  
    </script>
</body>
  
</html>


Output:
static method 1
static method 2

Example 3: The following example invokes more than one static method with same names.

HTML




<!DOCTYPE html>
<html>
  
<body>
    <script>
        class GeeksforGeeks {
  
            // static keyword is used 
            // before the function name
            static example1() {
  
                // returns static method 1
                return "static method 1"
            }
  
            // Different static method
            // with same name 
            static example1() { 
                  
                // returns static method 2
                return "static method 2"
            }
  
        }
  
        // Calls the static function using 
        // className.functionName
        document.writeln(GeeksforGeeks.example1());
        // Invokes last static method in case 
        // if static function have same names
    </script>
</body>
  
</html>


Output:
static method 2

Example 4: The following example demonstrates to invoke static method within the non static method.

HTML




<!DOCTYPE html>
<html>
  
<body>
  
    <script>
        class GeeksforGeeks { 
              
            // Static keyword is used 
            // before the function name
            static example1() { 
                  
                // return the string --> 
                // static method 1
                return "static method 1"
            }
  
            // Non static method with 
            // different name 
            example2() { 
                  
                // return the string -->
                // static method 1
                // Calls the static function 
                // using className.functionName
                document.writeln(GeeksforGeeks.example1());
            }
        }
        var gfg = new GeeksforGeeks();
        gfg.example2();  
    </script>
</body>
  
</html>


Output:
static method 1



My Personal Notes arrow_drop_up
Recommended Articles
Page :