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:

var variable_name:type = some value;

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

var myname:String = "Jesse";

This code consists of the keyword var, the variable definition (which states we are creating a new variable called 'myname'), the type of data (String) 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).

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).

Variable Types:

Variables are used to store a variety of different types of information, from street addresses to a person's age, so some form of distinction between variable types is required. Since the names we give our variables can be almost completely arbitrary, Flash needs some other indication as to the type of information we are storing, so that it is able to manipulate the information later on should we wish it. For instance, there is no point in trying to multiply two people's names together; such an operation is only reasonable for Numeric values. A variable's datatype relates to the information the variable stores and assists Flash in determining which actions are appropriate to invoke on this information. In this tutorial we will discuss the following basic datatypes: Strings, Numbers and Booleans.


Numbers are an easy concept to grasp; you've been using them since you were a child. Numeric variables in Flash are variables whose values can be manipulated using mathmetical expressions like multiplication. For instance, if we wished to record the year in which ActionScript.org was launched, we would generally do it as a number, in the following form:

var founding_year:Number = 2000;

If we want to determine how long this site has been operational and we have another variable which stores the current year as a numeric value, we can subtract the founding year from the current year to get a third numeric variable, as follows:

founding_year = 2000;
var   current_year:Number = 2003;
var   operational_for:Number = current_year - founding_year;

In the above example, the operational_for variable will be created an allocated the numeric value 3. Let's move on to some other variable types and the distinction should become clearer.


Strings are one or more characters (letters, digits, spaces, etc.) tied together. In general this includes things like names, addresses, and other information which can't be manipulated in the same way as numbers. String values are signified in Flash by enclosing the text we wish to store in our variable in double or single quotes. For example, the following statements both create a variable called country which stores the word Australia.

country = 'Australia';
country = "Australia";

If you were to enter either of the above lines without the use of quotes, you would be telling Flash to assign the variable called country the same value as the variable Australia.

Whenever you wish to represent a phrase, sentence or sequence of characters, you will generally use a String. Strings are also the type of data displayed in TextFields in Flash, although the TextField Object is beyond the scope of this tutorial.

If the distinction between Numeric variables and String variables is still not clear, consider the following example:

var my_sum:Number;
my_sum = 1 + 2 + 3;

Written in this form, the above ActionScript will create a new variable called my_sum, evaluate the expression on the right hand side of the statement, and assign the new variable a value of 6. However, if we wished to actually represent the equation rather than its result, we would do so using a String value, as the String will not be interpreted, rather, Flash will store it exactly as we provide it:

var my_sum:String;
my_sum = "1 + 2 + 3";


Boolean variables might be a foreign concept to you if you have never programmed before. Boolean variables store one of two values: true or false. When you get into more complicated ActionScript such as conditionals and loops you will use Boolean values often. For now just understand that they store a logical representation of either true or false which can be used in making decisions. If you wanted to create a Boolean variable which shows that you like Flash, you would do so as follows:

var likeFlash:Boolean;
likeFlash = true;

Strict Data Typing

ActionScript 2.0 in Flash MX (Pro.) 2004 introduced the concept of Strict Data Typing which allows developers to specify the single acceptable type for a variable when the variable is declared/defined. Should you inadvertently try to assign a value of the wrong type to a strictly typed variable, you will receive a "Type Mismatch" error when publishing your movie. To strictly type a variable you declare/define it as normal, then add a colon and the datatype after the variable name. You must also use the var operation at the beginning of your declaration/definition for scoping reasons.

var fav_color:String = "purple";

This declares and defines a new variable called fav_color which may only ever contain String values.

Strict Typing can also be applied to function return types and arguments:

function doSomething(word:String):Number {
... //code in here 

This defines a function called doSomething which must take a String argument and must return a Number.

Finally, note that when using Strict Date Typing you may bind a variable or function to custom datatypes(create your own classes; you are not limited to Flash's built-in types.


Now you know the basics of Flash variables. You know how to create them and the three major types. Paths are also important to learn about in relation to variables, so checkout my Paths tutorial to broaden your horizons. In the mean time, fiddle around with variables in any way you can think of!

Jesse Stratford is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash.


Adapted from


home / tutorials / Variables 101

Tutorial details:
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.