JScript  

Variable Scope

JScript has two scopes: global and local. If you declare a variable outside of any function definition, it is a global variable, and its value is accessible and modifiable throughout your program. If you declare a variable inside of a function definition, that variable is local. It is created and destroyed every time the function is executed; it cannot be accessed by anything outside the function.

Languages such as C++ also have "block scope." Here, any set of braces "{}" defines a new scope. JScript does not support block scopes.

A local variable can have the same name as a global variable, but it is entirely distinct and separate. Consequently, changing the value of one variable has no effect on the other. Inside the function in which the local variable is declared, only the local version has meaning.

var aCentaur = "a horse with rider,"; // Global definition of aCentaur.

// JScript code, omitted for brevity.
function antiquities() // A local aCentaur variable is declared in this function.
{

// JScript code, omitted for brevity.
var aCentaur = "A centaur is probably a mounted Scythian warrior";

// JScript code, omitted for brevity.
   aCentaur += ", misreported; that is, "; // Adds to the local variable.

// JScript code, omitted for brevity.
} // End of the function.

var nothinginparticular = antiquities();
aCentaur += " as seen from a distance by a naive innocent.";

/*
Within the function, the variable contains "A centaur is probably a mounted Scythian warrior,
misreported; that is, "; outside the function, the variable contains the rest of the sentence:
"a horse with rider, as seen from a distance by a naive innocent."
*/

It's important to note that variables act as if they were declared at the beginning of whatever scope they exist in. Sometimes this results in unexpected behaviors.

tweak();
var aNumber = 100;
function tweak()  {
    var newThing = 0;  // Explicit declaration of the newThing variable.

    // This statement assigns the value undefined to newThing because there is a local variable with the name aNumber.
    newThing = aNumber;

    // The next statement assigns the value 42 to the local aNumberaNumber = 42;
    if (false)  {
        var aNumber;  // This statement is never executed.
        aNumber = 123;  // This statement is never executed.
        }  // End of the conditional.

}  // End of the function definition.

When JScript executes a function, it first looks for all variable declarations,

var someVariable;

and creates the variables with an initial value of undefined. If a variable is declared with a value,

var someVariable = "something";

then it still initially has the value undefined, and will take on the declared value only when the line containing the declaration is executed, if ever.

JScript processes variable declarations before executing any code, so it does not matter whether the declaration is inside a conditional block or some other construct. Once JScript has found all the variables, it executes the code in the function. If a variable is implicitly declared inside a function - that is, if it appears on the left-hand-side of an assignment expression but has not been declared with var - then it is created as a global variable.