Categories Displayed in Flash

AS3: NaN as Nullable Numbers

One major change from AS2 was the removal of Numbers being nullable, this was useful for determining if something had been given a value or not, and setting it to a default. One workaround is using NaN, which can be used on the function initializer:

  1. function testNum(num:Number = NaN) {
  2. if (isNaN(num)) {
  3. trace(num + " no num");
  4. } else {
  5. trace("has num: " + num);
  6. }
  7. }
  8.  
  9. /////////// BEGIN TEST /////////////
  10. var numA:Number; //NaN
  11.  
  12. testNum(); // NaN no num
  13. testNum(numA); //NaN no num
  14. numA = -1;
  15. testNum(numA); //has num: -1
  16. numA = 0;
  17. testNum(numA); //has num: 0
  18. numA = 1;
  19. testNum(numA); //has num: 1

Add a Comment: