contents   index   previous   next

Variable scope


Variables in ScriptEase may be either global or local. Global variables may be accessed and modified from anywhere in a script. Local variables may only be accessed from the functions in which they are created. There are no absolute rules for preferring or using global or local variables. Each type has value. In general, programmers prefer to use local variables when reasonable since they facilitate modular code that is easier to alter and develop over time. It is generally easier to understand how local variables are used in a single function than how global variables are used throughout an entire program. Further, local variables conserve system resources.


To make a local variable, declare it in a function using the var keyword:


var perfectNumber;


A value may be assigned to a variable when it is declared:


var perfectNumber = 28;


The default behavior of ScriptEase is that variables declared outside of any function or inside a function without the var keyword are global variables. However, this behavior can be changed by the DefaultLocalVars and RequireVarKeyword settings of the #option preprocessor directive. This directive is explained in the section on preprocessing. For now, consider the following code fragment.


var a = 1;

function main()


   b = 1;

   var d = 3;




function someFunction(e)


   var c = 2




In this example, a and b are both global variables, since a is declared outside of a function and b is defined without the var keyword. The variables, d and c, are both local, since they are defined within functions with the var keyword. The variable c may not be used in the main() function, since it is undefined in the scope of that function. The variable d may be used in the main() function and is explicitly passed as an argument to someFunction() as the parameter e. The following lines show which variables are available to the two functions:


main(): a, b, d

someFunction(): a, b, c, e


It is possible, though not usually a good idea, to have local and global variables with the same name. In such a case, a global variable must be referenced as a property of the global object, and the variable name used by itself refers to the local variable. In the fragment above, the global variable a can be referenced anywhere in its script by using: global.a.


Function identifier