Hopefully you now understand the purposes of and differences between Booleans, Strings and Numbers. The test below should help you reinforce this knowledge a little:


Changing Variable Types

Being a scripting language, ActionScript is generally very obliging if you wish to convert a variable's value from one form to another or allocate a variable a value of another type. For instance, the following code, which creates a variable, assigns it a string value, then overwrites that value with a numeric value, is perfectly acceptable:
fav_color = "purple";
fav_color = 1;
This flexibility is often very useful but it can also make tracking down bugs caused by assigning variables the wrong type of value (which you later try to manipulate using the wrong operations) very difficult. For instance, if you accidentally assign a variable a string value and then try to add it to a numeric value, Flash makes both values Strings and concatenates them together. In a small script this isn't such an issue, but in a full Rich Internet Application it can be a bug finding nightmare. Strict Data Typing, addressed below, helps to avoid this issue.

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 {
}
This defines a function which must take a String argument and must return a Number.

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

Fiddling

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.

NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there.

If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity.
This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it.