Blog & News

The Ternary Operator in Flash (if-else shorthand)

girl learning coding in front of computer

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;

Popular Posts

Channels

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.

Show all iD Tech Locations Show all iD Tech Courses