[Previous] [Next] [First] [Last]


Statements

The sequence of execution of a JavaScript program is controlled by statements, which are executed for their effect and do not have values.

Some statements contain other statements as part of their structure; such other statements are substatements of the statement. We say that statement S immediately contains statement U if there is no statement T different from S and U such that S contains T and T contains U. In the same manner, some statements contain expressions as part of their structure.

6.1 Normal and Abrupt Completion of Statements

Every statement has a normal mode of execution in which certain computational steps are carried out. The following sections describe the normal mode of execution for each kind of statement. If all the steps are carried out as described, the statement is said to complete normally. However, the break, continue, and return statements cause a transfer of control that may prevent normal completion of statements that contain them.

If such an event occurs, then execution of one or more statements may be terminated before it completes normally; such statements are said to complete abruptly.

Unless otherwise specified, abrupt completion of a substatement causes the immediate abrupt completion of the statement itself, and all succeeding steps in the normal mode of execution are not performed. Unless otherwise specified, a statement completes normally if all substatements it executes complete normally.

6.2 Blocks

A block is a sequence of statements and variable declarations statements within braces.

Block:
          Statement
          { BlockStatementsopt }

BlockStatements:
          BlockStatement
          BlockStatements BlockStatement

BlockStatement:
          VariableDeclarationStatement
          Statement

A block is executed by executing each of the variable declarations and statements in order from first to last (left to right). If all of these block statements complete normally, then the block completes normally. If any of these block statements complete abruptly for any reason, then the block completes abruptly.

6.3 Variable Declaration Statements

Variables can be declared two ways in JavaScript:

A variable declaration statement declares one or more variable names.

VariableDeclarationStatement:
          VariableDeclaration ;

VariableDeclaration:
          var VariableDeclarators

The following productions are repeated here for clarity:

VariableDeclarators:
          VariableDeclarator
          VariableDeclarators , VariableDeclarator

VariableDeclarator:
          Identifier
          Identifier = AssignmentExpression

A variable declaration can also appear in the header of a for statement. In this case it is executed in the same manner as if it were part of a variable declaration statement.

Each declarator in a variable declaration declares one variable, whose name is the Identifier that appears in the declarator. If the variable has no initializer, it is undefined.

The scope of variable declarations is described in 3.5.2 Declaration and Visibility.

6.4 Statements

Some of the statements in the JavaScript language correspond to statements in Java, but some are unique to JavaScript.

Statement:
          EmptyStatement
          IfThenStatement
          WhileStatement
          ForStatement
          BreakStatement
          ContinueStatement
          ReturnStatement
          WithStatement
          ForInStatement

6.4.1 The Empty Statement

An empty statement does nothing.

EmptyStatement:
          ;

Execution of an empty statement always completes normally.

6.4.2 The if Statement

The if statement allows conditional execution of a statement or a conditional choice of two statements, executing one or the other but not both.

IfThenStatement:
          if ( Expression ) Block
          if ( Expression ) Block else Block

The Expression must be convertible to boolean, or a run-time error occurs. The Expression is evaluated and converted to boolean:

Because JavaScript parses top-down, it does not encounter any ambiguity. in this statement:

if (Condition1) 
          if (Ccndition2) 
                    Statement1
          else 
                    Statement2

The JavaScript top-down parser associates the else with the second if statement to form an if-else.

6.4.3 The while Statement

The while statement executes an Expression and a Statement repeatedly until the value of the Expression is false.

WhileStatement:
          while ( Expression ) Block

The Expression must be convertible to boolean, or a run-time error occurs.

A while statement is executed by first evaluating the Expression and converting it to boolean:

If the boolean conversion of the Expression is false the first time it is evaluated, then the Block is not executed.

6.4.3.1 Abrupt Completion

Abrupt completion of the contained Block is handled in the following manner:

6.4.4 The for Statement

The for statement executes some initialization code, then executes an Expression, a Block, and some update code repeatedly until the value of the Expression is false.

ForStatement:
          for ( ForInitopt ; Expressionopt ; ForUpdateopt ) Block

ForInit:
          Expression
          VariableDeclaration

ForUpdate:
          Expression

The Expression, if present, must be convertible to boolean, or a run-time error occurs.

6.4.4.1 Initialization

A for statement is executed by first executing the ForInit code:

6.4.4.2 Iteration

Next, a for iteration step is performed, as follows: If the Expression is present, it is evaluated and converted to boolean, and there is then a choice:

If the value of the Expression is false the first time it is evaluated, then the Block is not executed.

If the Expression is not present, then the for statement cannot complete normally; only abrupt completion (such as use of a break statement) can terminate its execution.

6.4.4.3 Abrupt Completion

Abrupt completion of the contained Block is handled in the following manner:

6.4.5 The break Statement

The break statement transfers control out of an enclosing statement.

BreakStatement:
          break ;

A break statement transfers control to the innermost enclosing while, for, or for/in statement. this statement, called the break target, then immediately completes normally. If no while or for statement encloses the break statement, a compile-time error occurs. A break statement always completes abruptly.

6.4.6 The continue Statement

The continue statement may occur only in an while, for, or for/in statement, known as an iteration statement. Control passes to the loop-continuation point of an iteration statement.

ContinueStatement:
          continue ;

A continue statement transfers control to the innermost enclosing iteration statement; this statement, called the continue target, then immediately ends the current iteration and begins a new one. If no iteration statement encloses the continue statement, a compile-time error occurs. A continue statement always completes abruptly.

6.4.7 The return Statement

The return statement returns control to the caller of a function.

ReturnStatement:
          return Expressionopt ;

A return statement with an Expression must be contained in a function definition or a compile-time error occurs.

A return statement with an Expression transfers control to the caller of the function; the value of the Expression becomes the value of the function call.

6.4.8 The with Statement

The with statement establishes the default object for a set of statements. See 3.5.1 Scope Resolution. Within the set of statements, any simple name (including the first part of a qualifed name) is resolved against the default object.

withStatement:
          with ( Expression ) Block

Expression is evaluated and converted to object. Block is then executed with the object pushed on a stack of default objects. The stack is popped after Block completes normally or abruptly. The with statement completes for the same reason that Block completes.

Example

The following with statement specifies that the Math object is the default object. The statements following the with statement refer to the PI property and the cos and sin methods, without specifying an object. JavaScript assumes the Math object for these references.

var a, x, y
var r=10
with (Math) {
   a = PI * r * r
   x = r * cos(PI)
   y = r * sin(PI/2)
}

6.4.9 The for/in Statement

The for/in statement iterates a specified variable over all the properties of an object. For each distinct property, JavaScript executes the specified statements in the Block.

ForInStatement:
          for (LeftHandSide in Expression) Block 

For each iteration of the loop, Expression is evaluated and converted to an object. For the first iteration of the loop, LeftHandSide is evaluated as in an AssignmentExpression, as described in 4.15.1 Simple Assignment Operator =, and is assigned the string identifier of the first property. Then, Block is executed. The second iteration occurs unless Block completed abruptly for the following reasons: break or return. The second iteration assigns the string identifier of the second property to LeftHandSide.

The loop continues until abrupt completion of Block due to break or return or until Block has completed with LeftHandSide set to the string identifier of the last property. Properties are ordered by the order in which they are set.

Example

The following function takes as its argument an object and the object's name. It then iterates over all the object's properties and returns a string that lists the property names and their values.

function dump_props(obj, obj_name) {
   var result = ""
   for (var i in obj) {
      result += obj_name + "." + i + " = " + obj[i] + "<BR>"
   }
   return result
}

6.4.10 Function Definition Statement

A function definition declares a JavaScript function name with the specified parameters.

FunctionDefinition:
          function Identifer ( ParameterListopt )           { BlockStatementsopt }

ParameterList:
          Identifier
          ParameterList , IdentifierName

A function definition statement cannot be nested inside another a function definition statement or any other statement.

Example

function fact(n) {
          if (n <= 1)
                    return 1
          return n * fact(n-1)
}


[Previous] [Next] [First] [Last]