Scripting: Flow Control

From Waltz
Jump to navigation Jump to search

This documentation is partially reproduced from the excellent Mozilla Developer Network Reference for JavaScript.

Flow control is the act of changing your scripts behavior based on some other variable or situation. JavaScript provides to primary methods of flow control: if…else statements and switch statements.

if…else

Use the if statement to execute a statement if a logical condition is true. Use the optional else clause to execute a statement if the condition is false. An if statement looks as follows:

if (condition) {
  statement_1;
} else {
  statement_2;
}

Here the condition can be any expression that evaluates to true or false. See Boolean for an explanation of what evaluates to true and false. If conditionevaluates to true, statement_1 is executed; otherwise, statement_2 is executed. statement_1 and statement_2 can be any statement, including further nested ifstatements.

You may also compound the statements using else if to have multiple conditions tested in sequence, as follows:

if (condition_1) {
  statement_1;
} else if (condition_2) {
  statement_2;
} else if (condition_n) {
  statement_n;
} else {
  statement_last;
} 

In the case of multiple conditions only the first logical condition which evaluates to true will be executed. To execute multiple statements, group them within a block statement ({ ... }) . In general, it's good practice to always use block statements, especially when nesting if statements:

if (condition) {
  statement_1_runs_if_condition_is_true;
  statement_2_runs_if_condition_is_true;
} else {
  statement_3_runs_if_condition_is_false;
  statement_4_runs_if_condition_is_false;
}

False Values

The following values evaluate to false (also known as Falsy values):

  • false
  • undefined
  • null
  • 0
  • NaN
  • the empty string ("")

All other values, including all objects, evaluate to true when passed to a conditional statement.

switch

A switch statement allows a program to evaluate an expression and attempt to match the expression's value to a case label. If a match is found, the program executes the associated statement. A switch statement looks as follows:

switch (expression) {
  case label_1:
    statements_1
    [break;]
  case label_2:
    statements_2
    [break;]
    ...
  default:
    statements_def
    [break;]
}

The program first looks for a case clause with a label matching the value of expression and then transfers control to that clause, executing the associated statements. If no matching label is found, the program looks for the optional default clause, and if found, transfers control to that clause, executing the associated statements. If no default clause is found, the program continues execution at the statement following the end of switch. By convention, the default clause is the last clause, but it does not need to be so.

The optional break statement associated with each case clause ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.

Expressions and Scripting
Concepts Script Expressions · JavaScript
Fundamentals Constructors · Functions · Grammar · Operations · Outputs · Parameters · Referencing Nodes
Advanced Topics Flow Control · Consumer Functions · Loops and Iterations · Reference Chains
Errors Recursion · Script Exceptions · Syntax Errors · Try and Catch