9.4.2.3   JavaScript Functions

Refer to the table below when creating JavaScript functions:

Function

JavaScript

Output

If statement

var vAge= [AGE];

var vReturn = true;

if ( isEmpty(vAge))

{

  vReturn = false;

}

The variable vReturn is set to false if the user does not enter a value for [AGE].

Notes:

·         The condition to be evaluated after the key word if must be enclosed in parentheses.

·         The use of Curly Brackets {} with the conditional code to be executed between them is recommended but if only a single statement is conditional, the curly brackets are optional.

Else...If statement

varvAge= [AGE];

var vReturn;

if ( isEmpty(vAge) || isNaN(vAGE)  ) {

  vReturn = false;

} else if ( Number(vAge) >= 18 && Number(vAge) <= 65 ) {

 vReturn = true;

} else {

 vReturn = false;

}

The variable vReturn is false if the value is not entered or if it is not Numeric, or if it falls outside the acceptable age for Age range.

Note:  You can tack on as many “else if” conditions as needed.

Switch statement

varvSit= [SITE];

switch ( vSit ) {

  case'Head':     vReturn= 'Eyes';      break;

  case'Torso':      vReturn = 'Chest';      break;

  default:      vReturn = 'None found';

}

For each case, all code between the colon (:) and the key-word break is executed when vSitcontains a value that matches the case.

The default block is optional as is the break key word on the final case (even if the final case is not default).

The use of the parentheses and curly brackets is not optional.

Warning: If you forget to use the break key word for any of your cases (before the last case), the code will 'fall through' and execute the next statement it finds.