Now comes the fun part—putting some data into our variables. If you're still playing along with the bank analogy, this is the "deposit money into our account" step. To assign a value to a variable, we use:
variableName = value;
where variableName is the name of a variable, and value is the data we're assigning to that variable. Here's an example:
bookTitle = "ActionScript for Flash MX: The Definitive Guide";
On the left side of the equals sign, the word bookTitle is the variable's name (its identifier). On the right side of the equals sign, the phrase "ActionScript for Flash MX: The Definitive Guide" is the variable's value—the datum you're depositing. The equals sign is called the assignment operator. It tells Flash that you want to assign (i.e., deposit) whatever is on the right of the equals sign to the variable shown on the left. If the variable on the left doesn't exist yet, Flash creates it (though relying on the interpreter to create variables implicitly isn't recommended).
Here are two more variable assignment examples:
speed = 25; output = "thank you";
The first example assigns the integer 25 to the variable speed, showing that variables can contain numbers as well as text. We'll see shortly that variables can contain other kinds of data as well. The second example assigns the text "thank you" to the variable output. Notice that we use straight double quotation marks (" ") to delimit a text string in ActionScript.
Now let's look at a slightly more complicated example that assigns the value of the expression 1 + 5 to the variable y:
y = 1 + 5;
When the statement y = 1 + 5; is executed, 1 is first added to 5, yielding 6, and then 6 is assigned to y. The expression on the right side of the equals sign is evaluated (calculated or resolved) before assigning the result to the variable on the left side. Here, we assign an expression that contains the variable y to another variable, z:
z = y + 4;
Once again, the expression on the right of the equals sign is evaluated, and the result is then assigned to z. The interpreter retrieves the current value of y (the interpreter checks the variable's account balance, so to speak) and adds 4 to it. Because the value of y is 6, z will be set to 10.
The syntax to assign any data—numbers, text, or any other type—to a variable is similar, regardless of the datatype. Although we haven't studied arrays yet, you should already recognize the following as a variable assignment statement:
myList = ["John", "Joyce", "Sharon", "Rick", "Megan"];
As before, we put the variable name on the left, the assignment operator (the equals sign) in the middle, and the new value to assign to the variable on the right.
To assign the same value to multiple variables in a hurry, we can piggyback assignments alongside one another, like this:
x = y = z = 10;
Variable assignment always works from right to left. The preceding statement assigns 10 to z, then assigns the value of z to y, then assigns the value of y to x.
In algebra, the following makes no sense, because something can't be equal to itself plus one:
x = x +1;
But in a programming language, this statement is perfectly valid and even common. It says to take the old value of x, add 1 to it, and store the new value back in the variable x. In this case, if the old value of x was 4, the statement would change x to 5.
The following statement is usually incorrect, because it changes the value of x to 5:
if (x = 5) { //do whatever};
Most likely, the programmer intended to compare the current value of x to 5:
if (x = = 5) { //do whatever};
See Chapter 5 for more details on the comparison operator (= =).