In JavaScript, variables fall into two major categories: global and local. Global variables are accessible from anywhere in the program and retain their values until the document is unloaded. Local variables, on the other hand, are created during function calls for temporary use and are accessible only within the functions that create them.
In this article I will demonstrate the use of global and local variables in JavaScript, explain the differences between them, and demonstrate why both types are useful. It is assumed that you are familiar with other aspects of JavaScript programming and that you know what a variable is.
If you are familiar with traditional structured programming languages, such as Pascal or C, you might see some strange things in this code:
year = 1997; function make_month_variable () { month = 2; } make_month_variable (); document . write ("year=" + year + " and month=" + month);
In JavaScript, newly created variables are assumed to be global, regardless of where they are created, unless explicit measures are taken to make them local. In the example shown above, the variable month is created when the function is called, not defined. After the function returns, however, the month variable retains its definition. (Otherwise, the write
statement would result in a JavaScript error.)
In summary, you should use global variables to store values that
var
. In the example shown below, a local variable named x is created and initialized with the value 5.
By the way, function parameter variables are considered local variables. (There are a few exceptions to this rule, but mentioning them here will only make things more complicated. :-)
function foo () { var x; x = 5; . . . }...or... function foo () { var x = 5; . . . }
Local variables are accessible only in the functions where they are created. When functions exit, all of their local variables are automatically destroyed.
Local variables fall out of scope in two ways:
Each time a function is entered, a separate "variable space" is created for the function's local variables. Thus, even when a function repeatedly calls itself, the local variable space created for the outer function call is unrelated to the variable space created for the inner function call.
When executed, the values of two distinct variables, both named x, will be printed. The following table describes the process:
function function_1 () { var x = 5; document . write ("
Inside function_1, x=" + x); function_2 (); document . write ("
Inside function_1, x=" + x); } function function_2 () { document . write ("
Inside function_2, x=" + x); } x = 1; document . write ("
Outside, x=" + x); function_1 (); document . write ("
Outside, x=" + x);
explanation | illustration | program output |
---|---|---|
A global variable named x is created and initialized with the value 1. This value is output to the document. | Outside, x=1 | |
function_1 is called. As it executes, a local variable named x is created and set to 5. Because this local variable has the same name as the global variable, the global variable is masked and the function's first write statement outputs the value 5, not 1.
|
Inside function_1, x=5 | |
function_1 calls function_2 . Because function_2 cannot see function_1 's variables, the reference to x in function_2 is interpreted as a reference to the global variable. Thus, a value of 1 is written to the document.
|
Inside function_2, x=1 | |
When function_2 returns, however, function_1 continues where it left off, and the local variable comes out of hiding. Thus, a value of 5 is again written to the document.
|
Inside function_1, x=5 | |
Finally, when function_1 finishes, the local variable x is destroyed and the global variable is the only one left. Thus, a value of 1 is written to the document.
|
Outside, x=1 |
Decades ago, when system memory was expensive, local variables were used to conserve memory. Variables were created on demand, used for a while, and then destroyed so that the memory used to store their values could be recycled for other variables. Although this once was a pretty good reason for using local variables, it is hardly worth mentioning in a JavaScript environment. There are better reasons than this.
Experienced programmers support a programming philosophy called "the principle of least privileges." This philosophy says that if the accessibility of a program resource, such as a function or variable, is restricted to just those portions of the program where such accessibility is required, then the programmer is less likely to introduce errors into the code without noticing them quickly.
Another good reason for using local variables is that less effort is required to keep track of variable names. Since each function call results in the creation of a new variable name space, different functions can use the same variable names without treading on each other's variables.
For these reasons, it is considered good programming practice to use local variables whenever possible. In JavaScript, this means your functions should use local variables to store temporary values that will not be needed after the function returns -- loop variables, for example. Adopting a habit of using local variables whenever possible will help you avoid subtle JavaScript coding errors that might not be noticed until after your web page is published.
Charlton Rose
17 Feb. 1997