The Sysadmin Notebook  

Sitemap

Flow Control

Flow Control in Javascript

Contents

If-Else

Top Bottom

The basic syntax for an if statement is:

if (condition) statement

The 'statement' can be a single statement or a block of statements (enclosed within curly brackets), to be executed only if 'condition' evaluates to true. >An 'if' statement can be followed by one or more 'else if' statements, to test for multiple conditions. A final else statement can be included, which will be executed if none of the preceding conditions are true.

    numb = prompt("Enter a number below 45");
    if (numb < 10) alert("less than 10");
    else if (numb < 20) alert("less than 20");
    else if (numb < 30) alert("less then 30");
    else if (numb <45) alert("less than 45");
    else alert("Time to exit");

Switch

Top Bottom

Multiple 'If-Else'statements are inefficient if testing the same variable against multiple conditions. The 'Switch' statement provides a more efficient control structure for such situations:

switch(test-expression) {
  case value1:
    statement
    break; 
  case value2:
    statement
    break; 
.
.
.
  case valueN:
    statement
    break;
  default:
    statement
    break;
}

The 'test-condition' for a switch loop can be a variable name or any other expression that can be evaluated. The test-value expression used with the case keyword be a number, string or other expression that produces a value. The test-value is compared to the test-expression using identity not equality, so no implicit conversions are used in the comparisons.

The 'break' statement causes the innermost closing loop to exit, and can appear in other loop structures. Without the 'break' statements, the switch block will test and execute for each matching condition. Break can also be followed by a label name, in which case execution will jump out of the labelled loop structure. To insert a label, simply insert an identifier followed by the semicolon - the semicolon is omitted when calling the label.

infiniLoop:
while (1 ) {
    var numb = prompt("Enter a number below 5");
    numb -= 0;
    switch(numb) {  
        case  1: alert("less than 2");
        break;
        case  2: alert("less than 3");
        break;
        case  3: alert("less than 4");
        break;
        case  4: alert("less than 5");
        break;
        default: alert("Time to exit");
        break infiniLoop;
    }
}

In the example above, there are two loops. The 'while' loop would keep running as long as '1' evaluates to 'true', which it always will by definition. However, if the user enters a number that does not convert to 1, 2, 3, or 4, the default action is taken: which is to break out of the while loop (labelled 'infiniLoop').

When using switch within a function, 'return' could be used instead of 'break'.

For

Top Bottom
for (initialise; loop test; increment) statement

for (index in array) statement

for (variable in object) statement

A 'continue' statement can be included in 'for' and 'while' loops, which causes execution of the current iteration to terminate, and continue on the next iteration

var myArray = new Array(1,2,3,4,5,6,7,8,9,10); 
for (var thirds in myArray) { 
    if (myArray[thirds] % 3) continue;
    alert(myArray[thirds]);
}

A 'for' loop with an empty statement can be used to initialise an array:

for (i = 0; i < myArray.length; i++ = 0) /* empty statement */ ;

While

Top Bottom
while (test expression) statement

'continue' can be used to terminate the remainder of a block and continue the loop at the next iteration, or break can be used to terminate the loop.

The do..while guarantees the code will execute at least once

do {statment} while (test expression)

The ternary operator

Top Bottom
var variable = test-expression ? trueValue:falseValue;

'variable' is set to 'trueValue' if 'test-expression' is true, otherwise 'variable' is set to 'falseValue'.