The syntax of a programming language is the set of rules you must follow when writing code. These rules dictate the symbols and words that can be used and how to structure your code. Syntax errors are some of the most common errors made by developers, especially when they are first learning a language. Typically, the compiler cannot compile code that contains syntax errors.

One thing to keep in mind—syntax does not provide information on the meaning behind the symbols or structure in your code. The semantics of a programming language provide the meaning behind the symbols, keywords, and structure. A program that is syntactically correct may not be semantically correct.

In this article, you will learn the syntax of ActionScript 3.

Case sensitivity

ActionScript 3 is a case-sensitive language. Identifiers that differ only in case are considered different identifiers. For example, the following code creates two different variables:

var sampleVariable:int; var SampleVariable:int;

Semicolons

The semicolon character ( ; ) is used to terminate a statement. If you omit the semicolon, the compiler assumes that each line of code represents a single statement. Terminating each statement with a semicolon is good practice and makes your code easier to read.

Parentheses

You can use parentheses (()) in three ways in ActionScript 3. First, you can use parentheses to change the order of operations in an expression. Operations that are grouped inside parentheses are always executed first. For example, parentheses are used to alter the order of operations in the following code:

trace(2 + 3 * 4); // 14 trace((2 + 3) * 4); // 20

Second, you can use parentheses with the comma operator ( , ) to evaluate a series of expressions and return the result of the final expression. This technique is shown in the following example:

var a:int = 2; var b:int = 3; trace((a++, b++, a+b)); // 7

Third, you can use parentheses to pass one or more parameters to functions or methods. In the following example, a String value is passed to the trace() function:

trace("hello"); // hello

Code blocks

One or more lines of code enclosed in curly brackets ( { } ) is called a block. Code is grouped together and organized into blocks in ActionScript 3. The bodies of most programming constructs like classes, functions, and loops are contained inside blocks.

function sampleFunction():void{ var sampleVariable:String = "Hello, world."; trace(sampleVariable); } for(var i:uint=10; i>0; i--){ trace(i); }

Whitespace

Any spacing in code—spaces, tabs, line breaks, and so on—is referred to as whitespace. The compiler ignores extra whitespace that is used to make code easier to read. For example, the following two examples are equivalent:

for(var i:uint=0; i<10; i++){ trace(i); } for(var i:uint=0; i<10; i++){trace(i);}

Comments

As you write ActionScript, you can leave notes to yourself or others. For example, use comments to explain how certain lines of code work or why you made a particular choice. Code comments are a tool you can use to write text that the computer ignores in your code. ActionScript 3 code supports two types of comments: single-line comments and multiline comments. The compiler ignores text that is marked as a comment.

Single-line comments begin with two forward slash characters ( // ) and continue until the end of the line. For example, the following code contains a single-line comment:

// a single line comment

Multiline or block comments begin with a forward slash and asterisk ( /* ) and end with an asterisk and forward slash ( */ ).

/* This is multiline comment that can span more than one line of code. */

Another common use of comments is to temporarily "turn off" one or more lines of code. For example, use comments to figure out why certain ActionScript code isn't working the way you expect by placing the code within comment syntax so that the compiler ignores it. You can also use comments to test a different way of doing something.

Literals

A literal is any fixed value that appears directly in your code. The following examples are literals:

17 "hello" -3 9.4 null undefined true false

Literals can also be grouped to form compound literals. The following example shows a compound literal being passed as a parameter to the Array class constructor.

var myStrings:Array = new Array(["alpha", "beta", "gamma"]); var myNumbers:Array = new Array([1,2,3,5,8]);

Keywords and reserved words

Reserved words are words that you cannot use as identifiers in your code because the words are reserved for use by ActionScript. Reserved words include lexical keywords, which are removed from the program namespace by the compiler. The compiler reports an error if you use a lexical keyword as an identifier. The following table lists ActionScript 3 lexical keywords.

Table 1. ActionScript 3 Lexical Keywords

as

if

return

break

implements

super

case

import

switch

catch

in

this

class

instanceof

throw

const

interface

to

default

internal

true

delete

is

try

do

native

typeof

else

new

use

extends

null

var

false

package

void

finally

private

while

for

protected

with

function

public

 

There is a small set of keywords, called syntactic keywords, that can be used as identifiers, but that have special meaning in certain contexts. The following table lists ActionScript 3 syntactic keywords.

Table 2. ActionScript 3 Syntactic Keywords

each

include

override

get

dynamic

static

set

final

 

namespace

native

 

There are also several identifiers that are sometimes referred to as future reserved words. These identifiers are not currently reserved in ActionScript 3. However, Adobe recommends avoiding these words because a subsequent version of the language may include them as keywords.

Table 3. ActionScript 3 Future Reserved Words

abstract

export

throws

boolean

float

to

byte

goto

transient

cast

intrinsic

type

char

long

virtual

debugger

prototype

volatile

double

short

 

enum

synchronized

 

Slash syntax

Slash syntax is not supported in ActionScript 3. Slash syntax was used in earlier versions of ActionScript to indicate the path of a movieclip or variable.

Where to go from here

Now that you are familiar with the syntax, learn other fundamentals of programming in ActionScript 3. Begin with ActionScript 3 fundamentals: Variables, ActionScript 3 fundamentals: Data types, ActionScript 3 fundamentals: Operators, and ActionScript 3 fundamentals: functions.

Acknowledgement

The content in this article is based on material originally published in the Learning ActionScript 3 user guide created by Adobe Community Help and Learning.