The Ternary Operator in Flash (if-else shorthand)

iD Tech in action

Most people fluent in Flash Actionscript are familiar with unary and binary operators, but there is also a ternary operator that can be quite useful. First, a refresh on operators:

Unary: count++;

Binary: count = count * 2;

Ternary: var result:String = (count == 0) ? "Empty" : "Not Empty");

So what does this mean? You can use the ternary form of the if statement as a shorthand. Let's look at the equivalent full if-else statement:

var result:String;
if (count == 0) result = "Empty";
else result = "Not Empty";

Of course, using the ternary operator cuts down on code readability, but three lines can effectively be cut down to one. Remember, the form is this:

(condition) ? ifTrueDoThis : ifFalseDoThis;

A photo of Ryan

Ryan manages blog content at iD Tech, starting with the company in 2008. He earned his MBA from Santa Clara University after obtaining his Bachelor’s degree from Arizona State. Connect on LinkedIn!

Featured Posts

Authors

About iD Tech

iD Tech is the #1 tech camp on the planet, with 150+ global locations. Kids and teens learn to code, design video games, produce videosengineer robots, and more!

We've bet our reputation on recruiting the top instructors in the country. Our guaranteed small classes ensure customized learning, leading to "a-ha moments" and awesome outcomes.

View all locations

View all courses