# Building Expressions

As we mentioned earlier in this lesson, an expression is a phraseor a collection of variables, numbers, text, and operatorsthat evaluates to a value. To understand this concept, take a look at this example:

```
var oneDozen:Number = 6 * 2;

```

To the right of the equals sign, you see 6 * 2this is an expression. When this script is executed, the expression 6 * 2 is replaced by the result of 6 multiplied by 2, or 12. Thus, oneDozen equals 12.

An expression can contain variables, arrays, even function callsanything that, when evaluated, returns a value. For example:

```
var total:Number = subTotal + tax;

```

The value of total is based on the result of adding the variable subTotal to the variable tax. The expression is subTotal + tax.

```
var discount:Number = totalPrice * employeeDiscount;

```

The value of discount is based on the result of multiplying the variable totalPrice by the value of the third element in the employeeDiscount array; totalPrice * employeeDiscount is the expression.

Here:

```
var usDollars:Number = 10;

var japaneseYen:Number = convertToYen(usDollars);

```

the value of japaneseYen is based on the value returned by a call to the convertToYen() functionmaking the function call itself the expression.

NOTE

Expressions are used to do the following:

• Set variable values

• Set array element values

• Determine whether conditions are being met (using the comparison operators)

• Dynamically name movie clips, variables, and objects

• Call functions dynamically

• And more

Expressions enable you to avoid hard-coding values that will remain the same no matter what. By assigning and manipulating values via expressions, you can make the data used by your scripts dynamicresulting in more interactive projects. Many of the scripts in this book rely on expressionsthat's because without them, your project plays back in exactly the same way each time it's viewed.  introduction  Lesson 1. Introducing ActionScript  Lesson 2. Using Event Handlers  Lesson 3. Understanding Target Paths  Lesson 4. Using Object Classes  Lesson 5. Using Functions  Lesson 6. Creating and Manipulating Data  Creating Variables  Creating Arrays  Creating Dynamic Text Fields and Retrieving Information  Retrieving the Data  Building Expressions  Operators  Manipulating Numerical Data Using Math  Manipulating Strings  What You Have Learned  Lesson 7. Creating Custom Classes  Lesson 8. Using Conditional Logic  Lesson 9. Automating Scripts with Loops  Lesson 10. Scripting UI Components  Lesson 11. Getting Data In and Out of Flash  Lesson 12. Using XML with Flash  Lesson 13. Validating Data  Lesson 14. Working with Text Fields  Lesson 15. Controlling Movie Clips Dynamically  Lesson 16. Time- and Frame-Based Dynamism  Lesson 17. Scripting for Sound  Lesson 18. Loading External Assets  Lesson 19. Testing and Debugging  Lesson 20. Maximum-Strength SWFs  Lesson 21. Printing and Context Menus