Understanding the ActionScript Language > About variables > Scoping a variable |  |

Scoping a variable

A variable's "scope" refers to the area in which the variable is known and can be referenced. There are three types of variable scope in ActionScript:

| Local variables are available within the function body in which they are declared (delineated by curly braces).
| Timeline variables are available to any Timeline if you use a target path.
| Global variables are available to any Timeline even if you do not use a target path.

You can use the `var` statement to declare a local variable inside a script. For example, the variables `i` and `j` are often used as loop counters. In the following example, `i` is used as a local variable; it only exists inside the function `makeDays`:

    function makeDays() {
    	var i;
    	for( i = 0; i < monthArray[month]; i++ ) {

    		_root.Days.attachMovie( "DayDisplay", i, i + 2000 );

    		_root.Days[i].num = i + 1;
    		_root.Days[i]._x = column * _root.Days[i]._width;
    		_root.Days[i]._y = row * _root.Days[i]._height;

    		column = column + 1;

    		if (column == 7 ) {

    			column = 0;
    			row = row + 1;
    		}
    	}
    }

Local variables can also help prevent name collisions, which can cause errors in your movie. For example, if you use `name` as a local variable, you could use it to store a user name in one context and a movie clip instance name in another; because these variables would run in separate scopes, there would be no collision.

It's good practice to use local variables in the body of a function so that the function can act as an independent piece of code. A local variable is only changeable within its own block of code. If an expression in a function uses a global variable, something outside the function could change its value, which would change the function.