Variables are elements that store data.
A good analogy for a variable is a box for carrying something around. It isn't the box itself that is important, it is whatever the box contains that is really important.
So, important to a variable is the data it holds.
Like there are many different types of box, depending on the thing they carry, the same there are many different types of variables.
A variable can contains a number, a string (words), a boolean (true or false) value, a URL(web address), an array (list of values).

Variables must be declared before they can be used. The syntax to create (declare) a variable is:
var variable_name:DataType = value;
var variable_name:DataType;
 variable_name = value;
- "var" is a reserved keyword used to declare a variable.
- "DataType" specifies the type of data that a variable contains. Although the DataType is not strictly required, it is considered best practice to specify the data type (will reduce the amount of memory that Flash will use to recognize the type of variable data).
The "variable_name" can be any name that follow these rules:   - Example:
// declare a variable that would hold a numerical value, and set this value to 78
var num:Number = 78;

// create a variable that would hold a string value, and then set a value
var str:String;
 str = "";

The name of the variable permits the access at its value and also can change the value if necessary.
You can assign the value of a variable to another variable.
  - Example:
// declare numerical variable
var num:Number = 78;

// declare another numerical variable, and assign it the value of "num"
var num2:Number = num;

// change the value of "num" variable
num = 89;

The main data type used in ActionScript are: Important: - becouse ActionScript is case-sensitive, you must follow the exact syntax. For example, "string" is different from "String".


The comments within the code are necessary when we want to specify the role and functions of certain variables or instructions, for easy understanding of the script later.
To add a comment on a single line, inside the code, use double slash //. All characters to the right of the double slash represent a comment.
  - Example:
// Comment on a single line
var a_var:String = "some text";

If you want to write comments on multiple lines, use /* at the beginning of the comment, and */ at its end.
  - Example:
/* comment on ...
 multiple lineas
another line ... */
var a_var:String = "some text";

The trace() function

The trace() function is a special kind of statement used to display information to the Output panel in Flash.
The trace statement is a useful tool for debugging the code, to check the values that a variable or function returns.
Like any function, you pass values to the trace() function by placing them inside the parentheses.
  - Syntax:
If you put a string inside the parentheses, like trace("Have a good life"), Flash shows that string in the Output panel when you test your presentation (with "Ctrl+Enter"), and the execution of your code reaches the trace() statement.
If you put a variable inside the parentheses, like trace(var_name), Flash shows the value of that variable in the Output panel.

- Here is an example with the elements presented in this tutorial (variables, comments and trace()):
  1. Open a new Flash document (ActionScript 3.0)
  2. Right-click on Frame 1 in the Timeline, and choose Actions
  3. In the "Actions panel" add the folloing code:
    /* Comments on multiple lines
     - ActionScript example: variables, comments and trace()
     - Web Site:
    // declares two variables (String and Number type)
    var str:String = 'AS3 Tutorial';
    var num:Number = 789;
    // using trace();
    trace('Current frame: '+ this.currentFrame);     // Checks the curent Frame number
    - Notice that you can add comments to a line with executable code, but after the end of that instruction.
    The parameter of the second "trace()" statement is a slightly more complex expresion.
    The '+' operator can be used to join string variables or text values together.
    The "this.currentFrame" returns the current frame number.
  4. Press "Ctrl+Enter". The Flash Player will display a blank window, important is the "Output panel" in Flash, which displays the results returned by the trace() function, as shown in the image below.
    Variables, Comments, trace
- The first trace() statement returns the value of the "str" variable ("AS3 Tutorial"), the second trace() outputs the value of the expression between brackets ("Current frame: 1").
The FLA file with this example can be downloaded from: Variables, Comments, trace.

Daily Test with Code Example

Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
Indicate the PHP variable that contains data added in URL address after the "?" character.
if(isset($_GET["id"])) {
  echo $_GET["id"];
Variables, Comments and trace()

Last accessed pages

  1. Multiple Select Dropdown List with AJAX (15558)
  2. Moving html element to a random direction (84)
  3. XHTML References - Old (15)
  4. Star shapes with CSS (4464)
  5. Get and Modify content of an Iframe (12618)

Popular pages this month

  1. Read Excel file data in PHP - PhpExcelReader (387)
  2. Register and show online users and visitors (355)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (327)
  4. Insert, Select and Update NULL value in MySQL (256)
  5. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (254)