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, starting with the company in 2008. He earned his MBA from Santa Clara University after obtaining his Bachelor’s degree from Arizona State. In terms of interests, Ryan possesses a fanatical addiction to Oakland A’s baseball, a borderline obsession with the music of John Mayer, and an undying commitment to Buffalo wings (and really anything else advertised as having Buffalo wing-flavoring).
Connect on LinkedIn!

Featured Posts

Authors

About iD Tech

iD Tech is the #1 tech camp on the planet, with 150+ locations nationwide. Kids and teens learn to code, design video games, produce videos, mod Minecraft, create with Roblox, engineer robots, model and print 3D characters, work with AI and machine learning, build laptops, learn about cybersecurity, and more!

We've bet our reputation on recruiting the top instructors in the country. Our 8:1 student-to-instructor average ensures customized learning with the smallest class sizes, leading to "a-ha moments" and awesome outcomes.

View all locations

View all courses