View Full Version : Where to declare vars?

06-09-2003, 05:22 PM
Hello :)

var one = 0;

function do_something()
var two = 1;

<onClick="do_something()"> // attached to a button

I know the variable 'one' gets created once as the webpage scripts are first 'read' by the browser.

Does a variable called 'two' get created every time the function do_something() is called?

some scripts i have seen have variables being created within functions and i thought they should be created outside of functions - maybe i have misunderstood something here


06-09-2003, 05:30 PM
I think... var two is a local variable. It only exists within the scope of the function it's declared in. Var one is a global one - it exists within the scope of the page, and can be accessed by any function. As a rule of thumb, declare vars globally (ie: outside functions) to reduce likelihood of "is null or is not an object" woes.

06-09-2003, 06:03 PM
thanks for the reply

does that mean that every time the do_something() function is called, a new variable called two is created, exists for the 'lifetime' of the function, then disappears after the function call is over.

in other words, if the button is clicked three times, are there three variables all called two - each having been created, used and then... who knows or just one that keeps being created, used and then destroyed?

could you clarify this for me?

thank you

06-09-2003, 06:23 PM
When a variable is declared inside of a function, it only lives as long as the function lives. So, let's say you call a function that has a variable defined in it. When the function is called, the variable is constructed and initialized. It is then accessed throughout the duration of the function call. When the function has completed, the variable is de-initialized and de-constructed and the memory space it was using is released.

So, in summary, if you have a variable that is inside of a function and call that function three times, the variable will be created and destroyed three times (Each time the function is called).

06-09-2003, 06:29 PM
Everything in client-side scripting is either an object or a property of an object (often both). So-called 'global' variables are actually properties of the window object, which, for all intents and purposes can be considered the global object in the client-side environment (the actual global object being inaccessible).

Every time you call a function, a call object belonging to that function object is created, and any variables local to the function - including parameter variables (arguments) become properties of it. Contrary to the above suggestion, it is not good practice to globally declare all your variables; those that are only relevant to a particular function, and will not need to hold their values between function calls, should always be declared (with the var keyword) within the function body. Omitting the keyword makes them global. Why clutter up the global (window's) namespace with unnecessary data? Enough floating around there already (see: IE).

Function-scope variable behavior is actually more complex in JS that it might seem; see crockford.com (http://www.crockford.com/javascript/private.html) for more about nested functions.

06-09-2003, 08:52 PM
thank you all

i understand now


06-10-2003, 09:31 AM
in vb you can declare a local variable as static and it will hold its value between calls to that method can you do the same in jscript

i have been studying java and understand that declaring a variable as static prevents an instance of a variable being created for instance of a class, does the same apply in jscript