How to create Static Variables in JavaScript ? Last Updated : 16 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report To create a static variable in JavaScript, you can use a closure or a function scope to encapsulate the variable within a function. This way, the variable maintains its state across multiple invocations of the function. Static keyword in JavaScript: The static keyword is used to define a static method or property of a class. To call the static method we do not need to create an instance or object of the class.Static variable in JavaScript: We used the static keyword to make a variable static just like the constant variable is defined using the const keyword. It is set at the run time and such type of variable works as a global variable. We can use the static variable anywhere. The value of the static variable can be reassigned, unlike the constant variable.Why do we create a static variable in JavaScript?In JavaScript, we simulate static variables using closures or other patterns to share state among instances, persist data between calls, or encapsulate information. These "static-like" variables enhance modularity and encapsulation, providing a way to maintain a state without relying on global variables. Example 1: In the below example, we will create a static variable and display it on the JavaScript console. JavaScript class Example { static staticVariable = 'GeeksforGeeks'; //static variable defined static staticMethod() { return 'static method has been called.'; } } // static variable called console.log(Example.staticVariable); // static method called console.log(Example.staticMethod()); OutputGeeksforGeeks static method has been called. Example 2: Static variable is called using this keyword. JavaScript class Example { static staticVariable = 'GeeksforGeeks'; //static variable defined static staticMethod() { return 'staticVariable : ' + this.staticVariable; } } // static method called console.log(Example.staticMethod()); OutputstaticVariable : GeeksforGeeks Comment More infoAdvertise with us Next Article How to create Static Variables in JavaScript ? V vikashgautam11 Follow Improve Article Tags : JavaScript Web Technologies javascript-functions JavaScript-Questions Similar Reads How to create a private variable in JavaScript ? In this article, we will try to understand how we could create private variables in JavaScript. Let us first understand what are the ways through which we may declare the variables generally in JavaScript. Syntax: By using the following syntaxes we could declare our variables in JavaScript. var vari 3 min read How to Create a String with Variables in JavaScript? To create a string with variables in JavaScript, you can use several methods, including string concatenation, template literals, and the String methods. Here are the common approaches:1. Template Literals (Recommended)Template literals are the modern and most preferred way to create strings with var 2 min read How to Declare Multiple Variables in JavaScript? JavaScript variables are used as container to store values, and they can be of any data type. You can declare variables using the var, let, or const keywords. JavaScript provides different ways to declare multiple variables either individually or in a single line for efficiency and readability.Decla 2 min read How to Pass variables to JavaScript in Express JS ? Express is a lightweight framework that sits on top of Node.jsâs web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your applicationâs functionality with middleware and routing. When working with Express.js you may encounter scenarios where you 2 min read How to declare Global Variables in JavaScript ? Global Variables Global variables in JavaScript are variables declared outside of any function. These variables are accessible from anywhere within the script, including inside functions. Global variables are declared at the start of the block(top of the program)Var keyword is used to declare variab 2 min read Like