The Flash ActionScript Variable

The Flash ActionScript Variable
The variable is the workhorse of any programming language. In Flash, it is one way that ActionScript can talk to the Flash program. Because ActionScript is based on the ECMAScript international standardized programming language for scripting, it makes sense that ActionScript will work in the same way as other scripting languages. So what is a variable and how does it work in ActionScript? You can think of a variable as a container or storage box for information. When you place information inside a variable, this is called assigning a value to the variable. The value, or information, will stay there until you change the value in some way by writing more ActionScript.

How do you create a variable in ActionScript? The easiest way is to use the variable in your program. Flash will recognize that it has never encountered this variable before and will make it "official". Although this method works and is perfectly acceptable, most programmers find it helpful to maintain more control. One way is by defining a variable using the var keyword. For example, let's create a variable that we will call "container" and assign to it an initial value to 20. Here is the code. Let's take a look at it and then break it down.

var container;
container = 20;

Notice that both lines of the code above end with a semicolon. The semicolon ends a line of program code much like a period ends a sentence. If you do not use the period at the end of your sentences, no one will be able to tell when one sentence ends and the next one begins. It's the same with programming. Don't forget those semicolons.

Let's take a look at the first line of code. It begins with the var keyword which is followed by the name we want to give to our variable. It is telling Flash that we are creating a new variable and we are naming it "container".

The second line of code assigns an initial value to the variable. This code might look familiar to those of you who have studied algebra. In algebra, x = 1 means that "x is equal to 1". The equal sign means "equal to" in algebra. However in ActionScript, the equal sign means "assign the value". Therefore, the second line is telling Flash to assign the value of 20 to the variable named "container".

The two lines of code above can be condensed into one line.
var container = 20;

Copyright 2018 Adobe Systems Incorporated. All rights reserved. Adobe product screen shot(s) reprinted with permission from Adobe Systems Incorporated. Adobe, Photoshop, Photoshop Album, Photoshop Elements, Illustrator, InDesign, GoLive, Acrobat, Cue, Premiere Pro, Premiere Elements, Bridge, After Effects, InCopy, Dreamweaver, Flash, ActionScript, Fireworks, Contribute, Captivate, Flash Catalyst and Flash Paper is/are either [a] registered trademark[s] or a trademark[s] of Adobe Systems Incorporated in the United States and/or other countries.

Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map

Content copyright © 2023 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Diane Cipollo for details.