Variables

Example: Declare and using variable

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
 
  <fx:Script>
    <![CDATA[
      // Declare the variable i and set it to 0.
      private var i:int = 0;
 
      /**
       * I don't why the statement below show the 
       *   error "Access of undefined property.".
       */
      //i = 0;
    ]]> 
  </fx:Script>
 
  <s:Button id="myButton" label="Clicked me!" x="100" y="100" height="25" width="100">
    <s:click>
      <![CDATA[
        /**
         * Everytime the button is clicked, 
         *  the color will change(RED, BLUE, RED, BLUE,...).
         */
        if(i%2==0){
          myButton.setStyle("color", "red");
          myButton.label="Red_"+i;
        }
        else{
          myButton.setStyle("color", "blue");
          myButton.label="Blue_"+i;
        }
        i++; // Increment i on every click.
      ]]>
    </s:click>
  </s:Button>
 
</s:Application>

Variables

Data type Default value Usage
Booleanfalse var myVar:Boolean = true;
int0 var myVar:int = 16;
NumberNaN (Not a number) var myVar:Number = 19756465.1654;
Objectnull var myVar:someClass = new someClass();
Stringnull var myVar:String = "Value of my string";
uint0 var myVar:uint = 4294967295;
Not declared (equivalent to type annotation *)undefined  
All other classes, including user-defined classes.null var myVar:yourClass = new yourClass();
Arraynull
var myVar:Array = ["Joe", "Peter", "Denis", "Ken"];
for(var i:int=0; i<myVar.length; i++)
{
  trace(myVar[i]);
}
 

Primitive vs Complex data type

In ActionScript 3.0, primitive values and their wrapper objects are indistinguishable. This means that the following two lines of code are equivalent:

var someInt:int = 3;
var someInt:int = new int(3);