Skip to content

12 Statements

Syntax

Statement :
Block
VariableStatement
EmptyStatement
ExpressionStatement
IfStatement
IterationStatement
ContinueStatement
BreakStatement
ReturnStatement
WithStatement

12.1 Block

Block :
{ StatementListopt }
StatementList :
Statement
StatementList Statement

Semantics

The production Block : { } is evaluated as follows:

1. Return "normal completion".

The production Block : { StatementList } is evaluated as follows:

  1. Evaluate StatementList .
  2. Return Result(1).

The production StatementList : Statement is evaluated as follows:

  1. Evaluate Statement .
  2. Return Result(1).

The production StatementList : StatementList Statement is evaluated as follows:

  1. Evaluate StatementList .
  2. If Result(1) is an abrupt completion, return Result(1).
  3. Evaluate Statement .
  4. If Result(3) is a value completion, return Result(3).
  5. If Result(1) is not a value completion, return Result(3).
  6. Let V be the value carried by Result(1).
  7. If Result(3) is "abrupt completion because of break ", return "abrupt completion after value V because of break ".
  8. If Result(3) is "abrupt completion because of continue ", return "abrupt completion after value V because of continue ".
  9. Return "normal completion after value V".

12.2 Variable statement

Syntax

VariableStatement :
var VariableDeclarationList ;
VariableDeclarationList :
VariableDeclaration
VariableDeclarationList , VariableDeclaration
VariableDeclaration :
Identifier Initializeropt
Initializer :
= AssignmentExpression

Description

If the variable statement occurs inside a FunctionDeclaration , the variables are defined with function-local scope in that function, as described in section 10.1.3. Otherwise, they are defined with global scope, that is, they are created as members of the global object, as described in section 0. Variables are created when the execution scope is entered. A Block does not define a new execution scope. Only Program and FunctionDeclaration produce a new scope. Variables are initialized to the undefined value when created. A variable with an Initializer is assigned the value of its AssignmentExpression when the VariableStatement is executed, not when the variable is created.

Semantics

The production VariableStatement : var VariableDeclarationList ; is evaluated as follows:

1. Evaluate VariableDeclarationList .

2. Return "normal completion".

The production VariableDeclarationList : VariableDeclaration is evaluated as follows:

1. Evaluate VariableDeclaration .

The production VariableDeclarationList : VariableDeclarationList , VariableDeclaration is evaluated as follows:

  1. Evaluate VariableDeclarationList .
  2. Evaluate VariableDeclaration .

The production VariableDeclaration : Identifier is evaluated evaluated by taking no action.

The production VariableDeclaration : Identifier Initializer is evaluated as follows:

  1. Evaluate Identifier .
  2. Evaluate Initializer .
  3. Call GetValue(Result(2)).
  4. Call PutValue(Result(1), Result(3)).

The production Initializer : = AssignmentExpression is evaluated as follows:

  1. Evaluate AssignmentExpression .
  2. Return Result(1).

12.3 Empty statement

Syntax

EmptyStatement :
;

Semantics

The production EmptyStatement : ; is evaluated as follows:

1. Return "normal completion".

12.4 Expression statement

Syntax

ExpressionStatement :
[lookahead ∉ {{, function}] Expression ;

Semantics

The production ExpressionStatement : Expression ; is evaluated as follows:

  1. Evaluate Expression .
  2. Call GetValue(Result(1)).
  3. Return "normal completion after value V", where the value V is Result(2).

12.5 The IF statement

Syntax

IfStatement :
if ( Expression ) Statement else Statement
if ( Expression ) Statement

Semantics

The production IfStatement : if ( Expression ) Statement else Statement is evaluated as follows:

  1. Evaluate Expression .
  2. Call GetValue(Result(1)).
  3. Call ToBoolean(Result(2)).
  4. If Result(3) is false , go to step 7.
  5. Evaluate the first Statement .
  6. Return Result(5).
  7. Evaluate the second Statement .
  8. Return Result(7).

The production IfStatement : if ( Expression ) Statement is evaluated as follows:

  1. Evaluate Expression .
  2. Call GetValue(Result(1)).
  3. Call ToBoolean(Result(2)).
  4. If Result(3) is false , return "normal completion".
  5. Evaluate Statement .
  6. Return Result(5).

12.6 Iteration statements

Syntax

IterationStatement :
while ( Expression ) Statement
for ( Expressionopt ; Expressionopt ; Expressionopt ) Statement
for ( var VariableDeclarationList ; Expressionopt ; Expressionopt ) Statement
for ( LeftHandSideExpression in Expression ) Statement
for ( var Identifier Initializeropt in Expression ) Statement

12.6.1 The while statement

The production IterationStatement : while ( Expression ) Statement is evaluated as follows:

  1. Let C be "normal completion".
  2. Evaluate Expressio n.
  3. Call GetValue(Result(1)).
  4. Call ToBoolean(Result(2)).
  5. If Result(3) is false , go to step 12.
  6. Evaluate Statement .
  7. If Result(6) is a value completion, change C to be "normal completion after value V " where V is the value carried by Result(6).
  8. If Result(6) is a break completion, go to step 12.
  9. If Result(6) is a continue completion, go to step 2.
  10. If Result(6) is a return completion, return Result(6).
  11. Go to step 2.
  12. Return C .

12.6.2 The for statement

The production IterationStatement : for ( Expressionopt ; Expressionopt ; Expressionop t ) Statement is evaluated as follows:

  1. If the first Expression is not present, go to step 4.
  2. Evaluate the first Expression .
  3. Call GetValue(Result(2)). (This value is not used.)
  4. Let C be "normal completion".
  5. If the second Expression is not present, go to step 10.
  6. Evaluate the second Expressio n.
  7. Call GetValue(Result(6)).
  8. Call ToBoolean(Result(7)).
  9. If Result(8) is false , go to step 19.
  10. Evaluate Statement .
  11. If Result(10) is a value completion, change C to be "normal completion after value V " where V is the value carried by Result(10).
  12. If Result(10) is a break completion, go to step 19.
  13. If Result(10) is a continue completion, go to step 15.
  14. If Result(10) is a return completion, return Result(10).
  15. If the third Expression is not present, go to step 5.
  16. Evaluate the third Expressio n.
  17. Call GetValue(Result(16). (This value is not used.)
  18. Go to step 5.
  19. Return C .

The production IterationStatement : for ( var VariableDeclarationList ; Expressionop t ; Expressionop t ) Statement is evaluated as follows:

  1. Evaluate VariableDeclarationList .
  2. Let C be "normal completion".
  3. If the second Expression is not present, go to step 8.
  4. Evaluate the second Expression.
  5. Call GetValue(Result(4)).
  6. Call ToBoolean(Result(5)).
  7. If Result(6) is false , go to step 15.
  8. Evaluate Statement .
  9. If Result(8) is a value completion, change C to be "normal completion after value V" where V is the value carried by Result(8).
  10. If Result(8) is a break completion, go to step 17.
  11. If Result(8) is a continue completion, go to step 13.
  12. If Result(8) is a return completion, return Result(8).
  13. If the third Expression is not present, go to step 3.
  14. Evaluate the third Expression .
  15. Call GetValue(Result(14)). (This value is not used.)
  16. Go to step 3.
  17. Return C.

12.6.3 The for..in statement

The production IterationStatement : for ( LeftHandSideExpression in Expression ) Statement is evaluated as follows:

  1. Evaluate the Expression .
  2. Call GetValue(Result(1)).
  3. Call ToObject(Result(2)).
  4. Let C be "normal completion".
  5. Get the name of the next property of Result(3) that doesn't have the DontEnum attribute. If there is no such property, go to step 14.
  6. Evaluate the LeftHandSideExpression ( it may be evaluated repeatedly).
  7. Call PutValue(Result(6), Result(5)).
  8. Evaluate Statement .
  9. If Result(8) is a value completion, change C to be "normal completion after value V" where V is the value carried by Result(8).
  10. If Result(8) is a break completion, go to step 14.
  11. If Result(8) is a continue completion, go to step 5.
  12. If Result(8) is a return completion, return Result(8).
  13. Go to step 5.
  14. Return C.

The production IterationStatement : for ( var Identifier Initializeropt in Expression ) Statement is evaluated as follows:

  1. If the Initializer is not present, go to step 6.
  2. Evaluate the Identifier .
  3. Evaluate the Initializer .
  4. Call GetValue(Result(3)).
  5. Call PutValue(Result(2), Result(4)).
  6. Evaluate the Expression .
  7. Call GetValue(Result(6)).
  8. Call ToObject(Result(7)).
  9. Let C be "normal completion".
  10. Get the name of the next property of Result(8) that doesn't have the DontEnum attribute. If there is no such property, go to step 19.
  11. Evaluate the Identifier (yes, it may be evaluated repeatedly).
  12. Call PutValue(Result(11), Result(10)).
  13. Evaluate Statement .
  14. If Result(13) is a value completion, change C to be "normal completion after value V " where V is the value carried by Result(13).
  15. If Result(13) a break completion, go to step 19.
  16. If Result(13) a continue completion, go to step 10.
  17. If Result(13) a return completion, return Result(13).
  18. Go to step 10.
  19. Return C .

The mechanics of enumerating the properties (step 5 in the first algorithm, step 10 in the second) is implementation dependent. The order of enumeration is defined by the object. Properties of the object being enumerated may be deleted during enumeration. If a property that has not yet been visited during enumeration is deleted, then it will not be visited. If new properties are added to the object being enumerated during enumeration, the newly added properties are not guaranteed to be visited in the active enumeration.

Enumerating the properties of an object includes enumerating properties of its prototype, and the prototype of the prototype, and so on, recursively; but a property of a prototype is not enumerated if it is "shadowed" because some previous object in the prototype chain has a property with the same name.

12.7 The CONTINUE statement

Syntax

ContinueStatement :
continue [no LineTerminator here] Identifieropt ;

An ECMAScript program is considered syntactically incorrect and may not be executed at all if it contains a continue statement that is not within at least one while or for statement. The continue statement is evaluated as:

1. Return "abrupt completion because of continue ".

12.8 The BREAK statement

Syntax

BreakStatement :
break [no LineTerminator here] Identifieropt ;

An ECMAScript program is considered syntactically incorrect and may not be executed at all if it contains a break statement that is not within at least one while or for statement. The break statement is evaluated as:

1. Return "abrupt completion because of break ".

12.9 The RETURN statement

Syntax

ReturnStatement :
return [no LineTerminator here] Expressionopt ;

An ECMAScript program is considered syntactically incorrect and may not be executed at all if it contains a return statement that is not within the Block of a FunctionDeclaration . It causes a function to cease execution and return a value to the caller. If Expression is omitted, the return value is the undefined value. Otherwise, the return value is the value of Expression .

  1. If the Expression is not present, return "abrupt completion because of return undefined ".
  2. Evaluate Expression .
  3. Call GetValue(Result(2)).
  4. Return "abrupt completion because of return V ", where the value V is Result(3).

12.10 The WITH statement

Syntax

WithStatement :
with ( Expression ) Statement

Description

The with statement adds a computed object to the front of the scope chain of the current execution context, then executes a statement with this augmented scope chain, then restores the scope chain.

Semantics

The production WithStatement : with ( Expression ) Statement is evaluated as follows:

  1. Evaluate Expression .
  2. Call GetValue(Result(1)).
  3. Call ToObject(Result(2)).
  4. Add Result(3) to the front of the scope chain.
  5. Evaluate Statement using the augmented scope chain from step 4.
  6. Remove Result(3) from the front of the scope chain.
  7. Return Result(5).

Discussion

Note that no matter how control leaves the embedded Statement , whether normally or by some form of abrupt completion, the scope chain is always restored to its former state.