This statement is used in do...while, for, for...in and while loops, as well as in switch blocks, to terminate the execution of the nearest enclosing block.
When a break
statement is encountered in a block, control is passed to the statement (if any) following the end of the nearest enclosing block that it appears in; unless the break
statement is followed by the name of a label, in which case control passes to the statement governed by the label.
In a for
loop.
for( var i = 0; i < limit; i++ ){ if( condition ) break; // ...statements } // ...statements
In a for
loop, nested with a label
.
label: for( var i = 0; i < limit; i++ ){ for( var j = 0; j < limit2; j++ ){ if( condition ) break label; // ...statements } // ...statements } // ...statement governed by label
In a switch
block, a break
statement is typically placed at the end of each case clause to prevent the interpreter from “falling through” (unless, of course, “falling through” is the desired effect). When the interpreter encounters a break
statement, control is passed to the statement following the nearest enclosing switch
block. If every clause has a corresponding break
statement, at most only one clause's statements will be executed. If the break
statement is followed by a label name (e.g. label
), when the break
is encountered, control is passed to the statement marked with that label.
switch( expression ){ case 1: // ...statements break; case 2: label: while( condition ){ if( expression ){ break label; } // ...statements } // *falls through* default: // ...statements break; } // ...statements
See also:
This statement serves as a condition label for switch blocks. For each possible condition a switch
statement's expression can evalute to, a case
is used to identify which statements within the block are to be executed. If the literal value of a case
clause matches the value of the switch
statements' expression, then that case
is executed.
A break statement is typically placed at the end of each case
clause to prevent the interpreter from “falling through” to the next clause. Omitting the break
statement for a given clause, causes every statement in the case
block to be executed, from the matching case
label to the first break
statement, or the end of the switch
block, including the default clause, whichever is encountered first.
switch( expression ){ case Number: // ...statements break; case String: // ...statements // *falls through* case Object: // ...statements // *falls through* default: // ...statements break; } // ...statements
See also:
This statement is used as a counterpart to try statements for handling exceptions that may occur. If an exception does occur in the statements within the try
block, the statements in the catch
block are executed. catch
blocks are available in two flavors, unqualified and qualified.
Qualified Exceptions:
Unqualified.
try{ // ...statements } catch( e ){ // ...statements }
Qualified.
try{ // ...statements } catch( e if e instanceOf EvalError ){ // ...statements }
See also:
This statement is used in do...while, for, for...in and while blocks to transfer control to the controlling-expression of the nearest enclosing block. All remaining statements in the current iteration are not executed. If a continue
statement is encountered in a for
block, execution immediately passes to the loop-expression and continues normally (e.g. the controlling-expression is tested, and if found to be true
, the statements in the block are executed).
In a for
loop.
for( initializing-expression; controlling-expression; loop-expression ){ // ...statements if( condition ) continue; // ...statements } // ...statements
If a continue
statement is encountered in a do…while
or while
loop, execution immediately passes to the controlling-expression, which is re-evaluated, and if true
, passes control back to the block for further execution.
do{ // ...statements if( condition ) continue; // ...statements } while( controlling-expression ); // ...statements
while( controlling-expression ){ // ...statements if( condition ) continue; // ...statements } // ...statements
See also:
This statement is used in switch blocks to define the default statements to be executed in the event that the switch
statement's expression doesn't evaluate to a case clause that has been defined. If no default
clause is defined and none of the case clause labels match, the switch
will not execute any statements within the block and control will be passed to the statement following the switch
block. Because each case
is evaluated in ascending order, if a default
clause is used, it should always be the last clause in the switch
block. Because default
is a “catch all” clause, if the interpreter reaches it, it will execute the associated statements.
It is customary to end a default
clause with a break statement.
switch( expression ){ case Number: // ...statements break; case String: // ...statements break; case Object: // ...statements break; default: // ...statements break; } // ...statements
See also:
This statement is used to create a loop that is guaranteed to execute the enclosed statements at least once. If the while
condition evalutes true
, control is passed back to the do
statement and the block is executed again. If the while
condition evaluates false
, control is passed to the statement immediately following the while
statement.
This statement is used in conjunction with the if statement to provide optional statements for execution. If no additional conditions exist, other than that used in the if
block, the else
keyword is used alone. However, if an additional condition does exist, the else
keyword is followed by the if
keyword and the corresponding condition.
Single condition.
if( condition ){ // ...statements } else{ // ...statements } // ...statements
Multiple conditions.
if( condition ){ // ...statements } else if( condition ){ // ...statements } else{ // ...statements } // ...statements
See also:
This statement is used in conjunction with the try and catch statements to handle errors and provide a sequence of statements to be executed, unconditionally, in the event that an error does occur. The statements enclosed within a try
block will be executed regardless of whether or not a return statement is encountered in the associated try
and/or catch
block(s).
try{ // ...statements } catch( e ){ // ...statements } finally{ // ...statements }
This statement is used to create a loop that executes for as long as an expression is true
, typically a fixed number of times.
A for
statement has three expressions that are used to control how the loop is executed:
The first expression is the initializing-expression. This statement is typically used to initialize (and possibly declare) the variable used in the controlling-expression.
The second expression is the controlling-expression. This statement is typically a conditional expression that is evaluated before each iteration of the loop (including before the first iteration). If this expression evaluates false
, the enclosed statements are not executed for that iteration and control is passed to the third expression. If the expression never evaluates true
the enclosed statements are never executed and control is passed to the statement immediately following the for
block.
The third part, the loop-expression, contains a statement that is executed at the end of every iteration. Typically, this part is used to increment the variable initialized in the first expression.
This statement is used to create a loop that executes for each element of an Array.
This statement is used to conditionally execute a sequence of statements, depending on the value of an expression. If the condition evaluates false
, control is passed to the statement immediately following the if
block. When used in conjunction with an else
and/or else if
block, only the statements enclosed in one of the blocks is executed before passing control to the statement immediately following the block(s).
It is generally considered good practice to enclose the sequence of statements governed by the condition in matching curly braces ({}), for the sake of clarity and to help avoid inadvertent errors. It is, however, valid (albeit not recommended) syntax to omit the curly braces if only one statement is governed by the condition.
Single condition.
if( condition ){ // ...statements } // ...statements
Single condition with optional else
block.
if( condition ){ // ...statements } else{ // ...statements } // ...statements
Multiple conditions.
if( condition ){ // ...statements } else if( condition ){ // ...statements } else{ // ...statements } // ...statements
See also:
Statements can be labelled. Labels are used to pass control from break and continue statements to a specific statement outside of the enclosing block.
OuterLabel: for( var i = 0; i < limit; i++ ){ InnerLabel: for( var j = 0; j < limit; j++ ){ // ...statements if( condition ){ // ...statements break OuterLabel; } else{ // ...statements continue InnerLabel; } } // ...statements } // ...statements
See also:
This statement is used to return from a function (with the exception of constructors), optionally returning a value from that function
. When a return
statement is encountered, control is passed to the statement following the one that called the function
. If the return
keyword is followed by an expression, the value of the expression is returned to the calling statement.
function demo( sArg ){ return String( "%1 was passed in." ).arg( sArg ); } var sTmp = demo( 5 ); // sTmp = "5 was passed in."
See also:
This statement is used to provide a multi-way branch by evaluating an expression (only once). It is used in conjunction with the case and default clauses to determine a statement, or sequence of statements, to execute based on whether the expression evaluates to one of the labels of the defined cases.
switch( expression ){ case Number: // ...statements break; case String: // ...statements break; case Object: // ...statements break; default: // ...statements break; } // ...statements
See also:
This statement is used to generate an error condition to be handled by a catch block. The exception thrown can be any expression.
// ...statements if( i > limit ){ throw "RangeError : Index out of range."; }
See also:
This statement is used to signify a statement, or sequence of statements, to attempt execution. It is used in conjunction with catch and finally blocks to handle errors that may occur.
try{ // ...statements } catch( e ){ // ...statements } finally{ // ...statements }
See also:
This statement is used to execute a statement, or sequence of statements, until a boolean expression evaluates false
. The expression is tested before each iteration of the loop, and if true
, executes the enclosed statement(s). If the expression evaluates false
, control is passed to the statement immediately following the enclosing block.
This statement is used to establish the default object for a block of code, to indicate that any unqualified names should be qualified with the provided object. It is commonly used to shorten the length of code that has to be written for a given segment of code.
More code.
// EXAMPLE NEEDED
Same result, less code.
// EXAMPLE NEEDED
Note: Leaving the interpreter to do the lookup for you may be slower than providing the fully qualified names.