Share your brilliance with the world by clicking the Add a Tip button... write a tip and win money!

Tip Details Add a Related Tip

Rate as: Positive Negative

Static variables in Javascript

Static variables are variables, typically used in functions, that maintain their value between function calls. Javascript does not support static variables per se; their is no static keyword in the language. However in javascript all functions are also objects and we can use this fact to simulate static variables. All we have to do is create a variable that is a member of the function, and since it's now part of an object, the value will be retained between calls. While there aren't truly static in the strictest sense of the word, they maintain their value between functions calls, and that usually serves the purpose.

As an example, consider a completely lame, but very instructive, function that keeps track of the number of times that it has been called. Here's how it might look:

function countMyself() {
    // Check to see if the counter has been initialized
    if ( typeof countMyself.counter == 'undefined' ) {
        // It has not... perform the initilization
        countMyself.counter = 0;
    }

    // Do something stupid to indicate the value
    alert(++countMyself.counter);
}

Each time that the countMyself function gets called from the page, the value is shown increasing by one. Using this trick, you can easily simulate static variables in Javascript.
Rating: 100% positive, 3 total Votes
Categories: javascript web programming
Added: on Aug 03, 2007 at 4:19 pm
Added By: an anonymous user

Comments on this Tip

Add a Comment
nice one!
– Added by an anonymous user on Aug 04, 2007 at 6:21 am
works great
– Added by an anonymous user on May 08, 2008 at 4:19 pm
Your Comment:
(how to format)
Rate This Tip:

Verify Humanity:
Sorry, we know it's annoying, but please enter the characters shown in the image to the left so that we know you're an actual person and not an evil spammer. Thanks.
       

Related Tips

Scrolling HTML elements using the keyboard

100% positive, 0 comments
– Tip added by rlansky on Jun 22, 2007 at 9:34 am

Telling when an iframe is done loading

100% positive, 0 comments
– Tip added by an anonymous user on Apr 18, 2008 at 8:05 am

Creating vector graphics in javascript

100% positive, 0 comments
– Tip added by Marcos84 on May 10, 2007 at 2:44 pm

Javascript variable naming

no ratings, 0 comments
– Tip added by an anonymous user on Sep 30, 2007 at 7:28 am

Code for creating Javascript CSS charts

no ratings, 0 comments
– Tip added by an anonymous user on Aug 21, 2007 at 12:23 pm

Efficient building of large strings in Javascript

no ratings, 0 comments
– Tip added by an anonymous user on Aug 08, 2007 at 12:52 pm

Avoid invalid title names when opening javascript windows

no ratings, 0 comments
– Tip added by an anonymous user on Aug 07, 2007 at 9:36 am

Nice article on javascript techniques

no ratings, 0 comments
– Tip added by rlansky on Apr 24, 2007 at 8:12 am

Using functions in conditionals in Smarty

Categories: php programming Smarty web
100% positive, 2 comments - last added on Sep 13, 2007 at 10:48 am
– Tip added by rlansky on Aug 10, 2007 at 1:02 pm

Something to consider before switching platforms

100% positive, 0 comments
– Tip added by an anonymous user on Sep 25, 2007 at 7:31 pm

Trimming whitespace in Javascript

Categories: javascript programming
100% positive, 0 comments
– Tip added by an anonymous user on Sep 13, 2007 at 1:54 pm

Copyright date on sites

Categories: web programming php Smarty
100% positive, 0 comments
– Tip added by an anonymous user on Mar 11, 2008 at 8:14 am

Wrap web text to any shape

100% positive, 0 comments
– Tip added by an anonymous user on Dec 16, 2007 at 10:35 am

Avoiding facebook timeout errors

Categories: Facebook programming web
100% positive, 0 comments
– Tip added by an anonymous user on Aug 21, 2007 at 5:21 pm

Declaring variables in Javascript

Categories: javascript web
100% positive, 1 comment - last added on Aug 17, 2007 at 8:08 am
– Tip added by an anonymous user on Aug 16, 2007 at 9:54 am

Javascript: How to Create a Random Number

no ratings, 0 comments
– Tip added by Walkere on Feb 23, 2008 at 9:49 pm

Don't use onChange events for checkboxes

Categories: HTML DOM javascript web
no ratings, 0 comments
– Tip added by an anonymous user on Oct 01, 2007 at 4:01 pm

Learn Python in 10 minutes

Categories: programming Python web
no ratings, 0 comments
– Tip added by Marcos84 on Jul 20, 2007 at 7:42 am

Tutorial for creating Facebook application

Categories: Facebook programming web
0% positive, 0 comments
– Tip added by an anonymous user on Jul 04, 2007 at 8:31 am

Sparkline code resources

Categories: graphics programming web
no ratings, 0 comments
– Tip added by an anonymous user on Jun 05, 2007 at 7:48 pm