Bits & Bytes

Declaring Variables and Constants in Actionscript with Fundamental Types

The basic format for declaring variables in actionscript looks like this:

var <variable name>:<variable type>;

We begin with var to indicate that we are declaring a variable. Then, we put a space, the variable name, a colon, the variable type, and, finally, a semi-colon to end the line. So, for example, we could declare a variable named MyNum of the type Number like this:

var dMyNum:Number;

A Number is used for floating-point or decimal numbers. We can initialize our variables in the declaration like this:

var dMyNum:Number = 3.14159;

Additionally, we can declare constants, which should be initialized, like this:

const kdMyNum:Number = 3.14159;

Actionscript has few a fundamental types to hold common data elements. These types are String, Boolean, Number, int, and uint. Strings are used to hold text. Booleans are used to hold the logical values “true” or “false”. Numbers are used to hold any number, including non-integer numbers. ints are used to hold integers only. uints are used to hold unsigned integers only. An example declaration and initialization of each of these types is shown here:

var sMyString:String = "XoaX.net";
var bMyBool:Boolean = true;
var dMyNum:Number = 3.14159;
var iMyInt:int = -67;
var uiMyUint:uint = 34;

Our first variable declaration did not contain an initial value. Variables that are declared with no initial value assigned to them take the following default values, according to their type:

String: null
Boolean: false
Number: NaN or not a number
int: 0
uint: 0

Tags: , , , , , , , ,

By: Michael Hall

Leave a Reply

*