Return booleans from functions

When returning a Boolean you can do the validation inside the return statement.

One way of doing it:

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

Or you could shorten it down to only one line, like this:

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

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

Latest articles