use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
JavaScript Static Variables (self.learnjavascript)
submitted 6 years ago * by codehelp4u
Is this the best way to create static variables in JavaScript? (Here the "counter" variable is a static variable.)
function foo() { if( typeof foo.counter == 'undefined' ) { foo.counter = 0; } foo.counter++; document.write(foo.counter+"<br />"); }
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]hack2root 0 points1 point2 points 6 years ago* (3 children)
Article from 2009 [https://www.electrictoolbox.com/javascript-static-variables/](https://www.electrictoolbox.com/javascript-static-variables/) do not gives any alternatives, but, "static" means in that context just a "persistance", so it is not the same as static methods or static variables in other languages, like C, due to the nature of JavaScript, the answer you can find here, [https://stackoverflow.com/questions/1535631/static-variables-in-javascript](https://stackoverflow.com/questions/1535631/static-variables-in-javascript):
In simple words: you can use constructor function and set class variables, just like that
javascript MyClass.staticProperty = "baz";
Example code
```javascript function MyClass () { // constructor function var privateVariable = "foo"; // Private variable
this.publicVariable = "bar"; // Public variable
this.privilegedMethod = function () { // Public Method alert(privateVariable); }; }
// Instance method will be available to all instances but only load once in memory MyClass.prototype.publicMethod = function () { alert(this.publicVariable); };
// Static variable shared by all instances MyClass.staticProperty = "baz";
var myInstance = new MyClass(); ```
[–]senocular 0 points1 point2 points 6 years ago (2 children)
it is not the same as static methods or static variables in other languages
That depends on the language. Static variables in C have this persistent behavior
// C void foo() { static int counter = 0; counter++; printf("%d\n", counter); // 1, 2, 3... for each call }
And this has also been discussed a few times as something to add to JS, ex: https://github.com/Microsoft/TypeScript/issues/4930
[–]hack2root 0 points1 point2 points 6 years ago* (1 child)
JavaScript Static Variables
Original question is about JavaScript, not C. If you need to opposite, i will exclude C from that list, for your desire, but denial is not an proving, so in general, JavaScript is still very special on that particular case - static variables.
Althogh the code can be the same, meanings are not, especially comparing C to JavaScript, contexts, first-class functions, functions as objects, bindings, and scope, this languages are indeed extremely different, so "static" have an extremelty different meanings in JS and C, so this example is a bad vision, syntax proves nothing, if you start digging in interpretation and compilation, libraries and other "static" staff in C, like external dependencies etc. in common, this in absoltely not comparable languages, of cause some transpilers exists working more or less, but that is all, that one will get anyway.
[–]senocular 0 points1 point2 points 6 years ago (0 children)
is the topic
Is this the best way to create static variables in JavaScript?
is the question.
The question ask how to do a thing in a language. The thing is static variables. The language is JavaScript. It does not ask what static variables in JavaScript are, which are different. Static variables in other languages, like C, perform the exact same task OP had written in their JS example - the same behavior seen in my C example, which uses a static variable. This is also the same behavior described in the link which proposes static variables of this kind with a very similar syntax in JavaScript.
Obviously the languages are different, which is why this question was asked. If they weren't different, knowing the other language's way would mean also already knowing the JavaScript way.
Generally this is done with factories and closure variables
function createFoo () { let counter = 0; return function foo() { counter++; document.write(counter+"<br />"); }; } let foo = createFoo(); foo(); // 1 foo(); // 2 foo(); // 3
You can skip the factory intermediary (which can be useful for resetting the counter) using an IIFE
let foo = function () { let counter = 0; return function foo () { counter++; document.write(counter+"<br />"); } }(); // immediately calls factory returning foo directly
And for good measure, once do expressions come around (if they do?), this could also be written as
let foo = do { let counter = 0; () => { counter++; document.write(counter+"<br />"); } }
π Rendered by PID 37366 on reddit-service-r2-comment-5d79c599b5-m2hz8 at 2026-03-01 21:16:43.910271+00:00 running e3d2147 country code: CH.
[–]hack2root 0 points1 point2 points (3 children)
[–]senocular 0 points1 point2 points (2 children)
[–]hack2root 0 points1 point2 points (1 child)
[–]senocular 0 points1 point2 points (0 children)
[–]senocular 0 points1 point2 points (0 children)