The stand-alone code can be found here : Var-Declaration-Test-Program Load it down and play with it ! Just reading my crap won't bring you very far ! Here is what The Sandman already told you about variables :Local variables are those variables that exist ONLY inside the function where it was created. Once program execution leaves the function then the variable(s) created inside this function are discarded and never used again. This makes good programming practice to use this kind of variable if it is not going to be needed elsewhere within the script.
Global variables once defined, exist within the entire script, they can therefore be changed or read by any function within the script itself and is often used as a kind of 'counter' and or to maintain the results from having a string or number processed by one or more functions. A short extension by me : - Within a function, a local declared variable has more rights than a global variable with the same name. So you can - within a function - pass a value to a global variable and change this local variable, under the condition that there was no local variable with the same name declared. - Within a function you cant't access variables, which are local variables in another function. So far, so good (isn't it ? :) Let me bring some light into this darkness (and show you some evil traps) : Javascript is a rather 'cool' language concerning the strictness of the use of variables. But this 'coolness' has its prize : you gotta be extremely careful with the use of the variables. Look at this code : (you should have a printing of _global.htm lying next to your console) var TestOne='Global_One' TestTwo='Global_Two' TestThree=null showglobal('Before'); var TestOne='Global_One'......:-- this is a DECLARATION TestTwo='Global_Two'...........:-- this is an ASSIGNMENT TestThree=null.....................:-- just another assignment We declare one variable and we assign two variables. Check this out :