A primitive data type is a type of data that isn't based on any other data types. The primitive data types we're going to use are as follows:

Number (e.g. 1, 3254, 123.045)

String (e.g. "My name is Joe Bloggs", "ActionScript")

Boolean (true or false)

ActionScript's data types are much simpler than most computer languages. For example, we don't have classes like 'int' or 'double' (like in Java) for numbers; any kind of number can be placed in the Number class. Also, there is no 'char' type - String is already a primitive data type, and if we want to store a character, we can put it in a string. There's only one kind of string: none of that CHAR/VARCHAR business in SQL.

Note the double quotes around the strings. One should always enclose a string with double quotes. The double quotes tells Flash that it's a string, not a variable name or anything else. true is a Boolean value, while "true" is a string.

The examples above are all examples of literals, which means they never change and aren't contained in a variable. For example, you can't change the value of 3254, can you?

Any data type that is not of a primitive data type is a composite data type. Composite data types are called classes.[2] The Object class is a prototypical object that has nothing inside it whatsoever. Under Object, we have many different classes. We will make our own classes in the third section. For now, we'll stick to using Flash's built-in classes.

Flash has a lot of built-in classes. We will discuss each class in detail in the second section. The most important ones to know are the MovieClip and Array classes.

Whenever we have an object of a particular class, we say that the object is an instance of the class. The name of an instance is, surprisingly enough, called an instance name.

Consider a variable a 'jar'. This jar can be used to store a lot of things. Sometimes, it stores an actual thing; other times, it stores a link to another thing that is too big to put into a jar. (We'll talk about this in the next chapter.)

Open Flash and type in the following code in the first frame, press (CTRL+ENTER for Windows or CMD+ENTER for Mac) to compile and run the script:

Code

Result

var Hello:String;
Hello = "Hello world!";
trace(Hello);

Hello world!

In human language, that means 'Create a new String called 'Hello', give it the value "Hello world!", then print Hello on the output box.

The first line is explained as follows:

The keyword var is put at the beginning of a line whenever you want to define a variable.

Hello is the name of your variable. (The technical term for a variable's name is identifier, but that is unnecessary jargon and we won't use that in this book.)

String is the data type of your variable. (The process of giving your variable a data type is called strong typing your variable.)[3]

The semi-colon is added to the end of each statement. A statement tells the computer to do something. In human language, 'Apples are green' is not a statement; 'eat that apple' is a statement. [4]

As you have probably noticed, the syntax for declaring a variable (i.e. telling the computer to set aside some of its precious RAM to store your variable) is as follows:

varvariableName:DataType;

Now let's take a look at the second line. The equal sign is called the assignment operator It is used to assign a value to a variable. In this case, since we're assigned the words 'Hello world!' to the variable.

The third line is a function which prints the words 'Hello world!' on the output screen.[5] A detailed explanation of functions will be given in the chapter on functions. For now, just take it for granted.

Like most programming languages, ActionScript does not allow certain names.

All variable names must start with a letter, an underscore (_) or a dollar sign($). Numbers are also allowed after the first character. No other characters should be used.

From Flash Player 7 on, variable names are case-sensitive. That means thisVariable and thisvariable are two distinct variables.

Reserved words (words that are used in Flash syntax) cannot be used as variable names.

Here's a list of reserved words:

add

and

break

case

catch

class

continue

default

delete

do

dynamic

else

eq

extends

false

finally

for

function

ge

get

gt

if

ifFrameLoaded

implements

import

in

instanceof

interface

intrinsic

le

it

ne

new

not

null

on

onClipEvent

or

private

public

return

set

static

super

switch

tellTarget

this

throw

try

typeof

undefined

var

void

while

with

These words may be used in future releases of Flash, so it's best not to touch them, either, lest your application should go wrong in future versions of Flash Player.

as

abstract

Boolean

bytes

char

const

debugger

double

enum

export

final

float

goto

is

long

namespace

native

package

protected

short

synchronized

throws

transient

use

volatile

In addition, according to naming conventions, variables should always start with a lowercase letter and consist of words smashed together using CamelCase. For example, instead of $Animalspecies, you should name the variable animalSpecies. There is an exception to this rule, which will be discussed later.

An additional convention for Boolean variables is to use a verb of the third person at the beginning. The most common type is verb to be + complement, such as isHappy, isPaused, etc. Other examples of good Boolean names include likesCheese, hasPimples, and so on.

In this code, the variable's data type is not defined. However, this is a frequent source of error for programmers who may then assign something else - like a number - to Hello. To avoid this error, it is highly recommended that you always declare the data type so that Flash will give you an error when you make such a mistake.

↑The semi-colon is not required in ActionScript (unlike some languages like PHP), but it is strongly recommended that you add it.

↑The output screen will pop up during runtime if you don't already have it in place.