Using Expressions in ActionScript

a0c1753e1e065f3c37d6aca8ec49dc72

Throughout every day life, we use expressions all of the time; however, we just don’t really think about them. We all learned how to use expressions while learning simple mathematical functions in elementary school (addition, subtraction, multiplication, and division), but we take for granted the fact that we use them now more in reality while we did when we were kids.

Mathmetical expressions can easily be utilized in ActionScript in Flash (nor any programming language for that matter). I have provided an example of some expressions that can be utilized in ActionScript below:

1 + 2
"a" / "b"
x * Variable

Expressions can also be placed within ActionScript functions. For example, a simple expression can be placed within the trace command. An example of this is shown below:

trace (1 + 2);

Do note that when executing your Flash document at this point, the result of the expression will display in the “Output” section, and not on the Stage. More on the trace command can be read about here.

Another way that expressions can be used is within variable assignments (using the var statement). An example of how this can be coded is provided below:

var xPrice = 5;
var xRate = 15;
var xInterest = xPrice * xRate;

Utilizing expressions throughout your ActionScript coding will give your Flash program more mathematical depth, and is a great fundamental to have when coding in other programming languages as well.

About the Author:
Chad Sweely is a staff writer and Flash Designer for FlashNewz.