Never Stop Learning

I’ve been a developer for over 10 years now and I stumbled upon an extremely simple bit of coding that I’ve never either seen or realized was out there. It’s so simple that I feel like a complete n00b for not knowing this!

So What Is It?

When toggling the value of a Boolean variable in ActionScript 3.0 you can set it to the opposite of what it currently is by doing the following:

var myVar:Boolean = false;
trace(myVar); // outputs: false
trace(!myVar);// outputs: true

Now, I’ll admit this isn’t mind altering stuff, it’s simply a short way to do the following:

var myVar:Boolean = false;
if (myVar == false) { myVar = true; }
else { myVar = false; }

or

var myVar:Boolean = false;
myVar = (myVar == false) ? true : false;

vs

var myVar:Boolean = false;
myVar = !myVar;

Where this comes into play is when you’re doing button toggles or updating the status of a Boolean variable where the expected result is the opposite of the current value.

Conclusion

The point of this post was to humble myself and remind me not to get comfortable with my level of expertise and to continue learning and exploring the languages and technologies I currently know, as well as new ones. I hope you all will do the same!

Cam Tullos
About the Author:

Cam is an accomplished developer and Wordpress Evangelist with a decade of expertise.
Follow Me


You must be logged in to post a comment.