Static Methods in JavaScript - GeeksforGeeks

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>
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>
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>
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>
static method 1

javascript-basics
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
JavaScript | Promises
How to get character array from string in JavaScript?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript