Written by: Jesse Stratford
Time: 30 - 45 minutes
Difficulty Level: Beginner
Requirements: Flash 5 or higher.
Topics Covered: What variables are and how we use them in Flash Actionscript.
Assumed knowledge: Very little.
The purpose of this tutorial is to provide a fundamental understanding of the purpose and application of variables in Flash ActionScript (both 1.0 and 2.0). This tutorial is aimed at beginners with no programming experience but is also suitable for programmers accustomed to other languages who wish to learn about Flash's syntax.

What is a Variable?

A variable is the simplest means of keeping track of information in your Flash code. A variable's value can be set once and never changed, or changed often. Values can be set during author-time (when you're creating your Flash file), or determined during run-time, like the score in a game.

Variables can be thought of as named boxes in which we store information. In order to access the information again we need to know the name of the variable which holds it. We provide the name by which the variable can be referenced when we create (or declare) our variable. Declaration let's Flash know that we'd like to create a new variable with a set name to store information in. Of course, a variable with no value is of little use, so generally we wish to define our variable also. Defining a variable is the act of allocating a value to the variable. In Flash, declaration and definition are generally combined into one statement of the form:

variable_name = some value;
For example, to record that my name is Jesse, we could declare and define a new variable called 'myname' as follows:

myname = "Jesse";
This code consists of the variable definition (which states we are creating a new variable called 'myname') and the value of this variable which is the string "Jesse", (more on strings later). The semi-colon at the end of the line signifies the end of our ActionScript command (or statement). Flash is quite forgiving if you leave off semi-colons at the end of statements, but strictly-speaking they are required.

A Note On Variable Naming:

In the above example, the variable name myname is purely arbitrary; we could have called it 'name' or pretty much anything else. Note however that variable names may not contain spaces or be the same as a reserved Flash keyword (see Macromedia's documentation for more information). Some people also consider it 'bad form' to begin a variable with a number or use hyphens in variable names (instead perhaps use an underscore).

Try the basic test below. Take careful note of things like the quotes around the name and the semi colon at the end of the line:

Assuming you got that one right, let's move on.