JS Jump Statements

A statement is a discrete instruction in a script that causes something to happen.

The statements in JavaScript can be classified into several categories. Here are the basic set of classifications:

Block - Some code enclosed in braces.

Variable statement - A declaration of a local or global variable.

Empty statement - A semi-colon on its own.

Just Statements
A jump statement is one, which forces the flow of execution to jump unconditionally to another location in the script.

Jump statements in JavaScript are used to terminate iteration statements.

A function call causes execution to go unconditionally to a new location (the beginning of the function's script source text block - its body) but a function call is not strictly a jump statement because the flow of control returns eventually to the line forllowing the function call.

A return statement is considered to be a jump statement.

The Break Statement
Used to unconditionally exit from a loop or a switch.

The break keyword is a 'jump' statement. It is used in an loop to abort the current cycle and exit from the smallest enclosing loop immediately. Execution continues at the line following the statement block associated with the loop.

A break statement can only legally exist inside a while or for loop in an ECMA-compliant implementation. Implementations that provide additional iterator types may also honor the same behavior for the break statement.

The break statement would normally be executed conditionally, otherwise it would cause the remaining lines in the loop to be redundant, since no execution flow would ever reach them. Compilers generally warn you about this, but JavaScript would simply ignore it.

At version 1.2 of JavaScript, the break statement was enhanced to support a label as a breaking destination. When the break is processed, it will jump to the end of the statement that has been labeled. If an iterator is labeled, then the break is associated with that iterator. This mechanism works like a 'goto'. It can work with an if block and with a labeled block of brace delimited code.

The continue statement
It is used to force the next iteration of a loop (as long as the condition has not been satisfied yet) or to execute the next command.

goto (Reserved word)
Reserved for future language enhancements.

This keyword suggests that future versions of JavaScript may support the goto statement which will unconditionally go to a labelled portion of script source text. This also suggests that labels will need to be supported as well.

ECMA edition 3 already mandates that case and default labels are supported for the benefit of the switch statement.

The ECMA standard notes that although it is reserved future use, an implementation is still compliant if it provides the appropriate functionality of these reserved keywords.

The Return Statement
Returns control back to the caller of a function.

A return keyword is a jump statement. It is used to unconditionally exit from a function, pass back a result, and make execution flow to the caller of the function.

When the return statement is executed, the execution context is disposed of and removed from the stack. Execution continues at the point in the caller where the function was invoked. The function is replaced by the value being returned.

If the function is not being assigned to an LValue or Left-Hand Side expression or has been cast to a void type, the result will be discarded.

If the expression is omitted in the return statement, the undefined value is returned in its place. While compiled languages are far more particular about the presence or absence of this expression, JavaScript is far more forgiving.

Functions that return undefined values are likely to be used as procedures rather than functions. A procedure is invoked as a statement that stands alone. The intent of a function is to return a result that will be substituted in its place.

It is considered illegal for the return statement to be present in any statement block other than that belonging to a function. However it can exist inside the statement block associated with a conditional statement or iterator statement as long as they themselves are within a function block. They may be nested more than one level deep but must ultimately belong to a function. Warnings:

It is considered to be a syntax error to use the return statement anywhere other than in a function body.

You will not get the return value back properly if there is a line terminator between the return keyword and the value is was supposed to return. There is a temptation to break long strings over several lines like this:

return

"A very long string goes here ...";

This will return the value undefined and not the string you intended to return. It is probably better style to assign the string to a variable and return that but there are implications there of string construction-destruction, garbage collection, and potential memory leaks and to trade those problems off it's best to try and eliminate string creation and memory usage if possible.

Example code:

// Declare a procedure with an implied return function aProcedure {  document.write("Hello"); }  // Declare a procedure that returns an undefined value function anotherProcedure {     alert("Click OK to continue"); return; }  // Declare a function that returns a result function aRealFunction {     return 1000; }  // Use the functions and procedures aProcedure; anotherProcedure; x = aRealFunction;