Mar 21 2010

AS3 snippet #4: Returning Booleans

Thomas Viktil

When returning a Boolean you can do the validation inside the return statement. Usually I would do something like this:

function doTest():Boolean
{
    var returnValue:Boolean = false;
    if (someValue > otherValue) { returnValue = true; }
    return returnValue;
}

Instead, you could shorten it down to only one line of code like this:

function doTest():Boolean
{
    return (someValue > otherValue);
}

doTest will return true if someValue is greater than otherValue. Pretty neat huh?