12 Statements
Syntax
- Statement :
- Block
VariableStatement
EmptyStatement
ExpressionStatement
IfStatement
IterationStatement
ContinueStatement
BreakStatement
ReturnStatement
WithStatement
12.1 Block
Syntax
- 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:
- Evaluate StatementList.
- Return Result(1).
The production StatementList : Statement is evaluated as follows:
- Evaluate Statement .
- Return Result(1).
The production StatementList : StatementList Statement is evaluated as follows:
- Evaluate StatementList.
- If Result(1) is an abrupt completion, return Result(1).
- Evaluate Statement.
- If Result(3) is a value completion, return Result(3).
- If Result(1) is not a value completion, return Result(3).
- Let V be the value carried by Result(1).
- If Result(3) is "abrupt completion because of break ", return "abrupt completion after value V because of break ".
- If Result(3) is "abrupt completion because of continue ", return "abrupt completion after value V because of continue ".
- 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 10.1.3) using property attributes { DontDelete }.. 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 initialised 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:
- Evaluate VariableDeclarationList.
- Return "normal completion".
The production VariableDeclarationList : VariableDeclaration is evaluated as follows:
1. Evaluate VariableDeclaration.
The production VariableDeclarationList : VariableDeclarationList , VariableDeclaration is evaluated as follows:
- Evaluate VariableDeclarationList.
- Evaluate VariableDeclaration.
The production VariableDeclaration : Identifier is evaluated as follows:
1. Return a string value containing the same sequence of characters as in the Identifier .
The production VariableDeclaration : Identifier Initializer is evaluated as follows:
- Evaluate Identifier as described in section 11.1.2.
- Evaluate Initializer.
- Call GetValue(Result(2)).
- Call PutValue(Result(1), Result(3)).
- Return a string value containing the same sequence of characters as in the Identifier .
The production Initializer : = AssignmentExpression is evaluated as follows:
- Evaluate AssignmentExpression.
- 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:
- Evaluate Expression .
- Call GetValue(Result(1)).
- 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
Each else for which the choice of associated if is ambiguous shall be associated with the nearest possible if that would otherwise have no corresponding else .
Semantics
The production IfStatement : if ( Expression ) Statement else Statement is evaluated as follows:
- Evaluate Expression .
- Call GetValue(Result(1)).
- Call ToBoolean(Result(2)).
- If Result(3) is false , go to step 7.
- Evaluate the first Statement .
- Return Result(5).
- Evaluate the second Statement .
- Return Result(7).
The production IfStatement : if ( Expression ) Statement is evaluated as follows:
- Evaluate Expression .
- Call GetValue(Result(1)).
- Call ToBoolean(Result(2)).
- If Result(3) is false , return "normal completion".
- Evaluate Statement .
- Return Result(5).
12.6 Iteration statements
An iteration statement consists of a header (which consists of a keyword and a parenthesized control construct) and a body (which consists of a Statement ).
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:
- Let C be "normal completion".
- Evaluate Expressio n.
- Call GetValue(Result(1)).
- Call ToBoolean(Result(2)).
- If Result(3) is false , go to step 12.
- Evaluate Statement .
- 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).
- If Result(6) is a break completion, go to step 12.
- If Result(6) is a continue completion, go to step 2.
- If Result(6) is a return completion, return Result(6).
- Go to step 2.
- Return C .
12.6.2 The for statement
The production IterationStatement : for ( Expressionopt ; Expressionopt ; Expressionop t ) Statement is evaluated as follows:
- If the first Expression is not present, go to step 4.
- Evaluate the first Expression .
- Call GetValue(Result(2)). (This value is not used.)
- Let C be "normal completion".
- If the second Expression is not present, go to step 10.
- Evaluate the second Expressio n.
- Call GetValue(Result(6)).
- Call ToBoolean(Result(7)).
- If Result(8) is false , go to step 19.
- Evaluate Statement .
- 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).
- If Result(10) is a break completion, go to step 19.
- If Result(10) is a continue completion, go to step 15.
- If Result(10) is a return completion, return Result(10).
- If the third Expression is not present, go to step 5.
- Evaluate the third Expressio n.
- Call GetValue(Result(16). (This value is not used.)
- Go to step 5.
- Return C .
The production IterationStatement : for ( var VariableDeclarationList ; Expressionop t ; Expressionop t ) Statement is evaluated as follows:
- Evaluate VariableDeclarationList.
- Let C be "normal completion".
- If the second Expression is not present, go to step 8.
- Evaluate the second Expression.
- Call GetValue(Result(4)).
- Call ToBoolean(Result(5)).
- If Result(6) is false , go to step 15.
- Evaluate Statement.
- 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).
- If Result(8) is a break completion, go to step 17.
- If Result(8) is a continue completion, go to step 13.
- If Result(8) is a return completion, return Result(8).
- If the third Expression is not present, go to step 3.
- Evaluate the third Expression.
- Call GetValue(Result(14)). (This value is not used.)
- Go to step 3.
- Return C .
12.6.3 The for..in statement
The production IterationStatement : for ( LeftHandSideExpression in Expression ) Statement is evaluated as follows:
- Evaluate the Expression .
- Call GetValue(Result(1)).
- Call ToObject(Result(2)).
- Let C be "normal completion".
- 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.
- Evaluate the LeftHandSideExpression ( it may be evaluated repeatedly).
- Call PutValue(Result(6), Result(5)).
- Evaluate Statement .
- 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).
- If Result(8) is a break completion, go to step 14.
- If Result(8) is a continue completion, go to step 5.
- If Result(8) is a return completion, return Result(8).
- Go to step 5.
- Return C .
The production IterationStatement : for ( var VariableDeclaration in Expression ) Statement is evaluated as follows:
- Evaluate VariableDeclaration.
- Evaluate Expression.
- Call GetValue(Result(2)).
- Call ToObject(Result(3)).
- Let C be "normal completion".
- Get the name of the next property of Result(4) that doesn't have the DontEnum attribute. If there is no such property, go to step 15.
- Evaluate Result(1) as if it were an Identifier; see 11.1.2 (yes, it may be evaluated repeatedly).
- Call PutValue(Result(7), Result(6)).
- Evaluate Statement.
- If Result(9) is a value completion, change C to be "normal completion after value V" where V is the value carried by Result(9).
- If Result(9) a break completion, go to step 15.
- If Result(9) a continue completion, go to step 6.
- If Result(9) a return completion, return Result(9).
- Go to step 6.
- 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 ;
Semantics
An ECMAScript program is considered syntactically incorrect if it contains a continue statement that is not within a 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 ;
Semantics
An ECMAScript program is considered syntactically incorrect if it contains a break statement that is not within a 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 ;
Semantics
An ECMAScript program is considered syntactically incorrect 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 .
The production ReturnStatement :: return [no LineTerminator here] Expressionopt ; is evaluated as:
- If the Expression is not present, return "abrupt completion because of return undefined ".
- Evaluate Expression .
- Call GetValue(Result(2)).
- 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:
- Evaluate Expression .
- Call GetValue(Result(1)).
- Call ToObject(Result(2)).
- Add Result(3) to the front of the scope chain.
- Evaluate Statement using the augmented scope chain from step 4.
- Remove Result(3) from the front of the scope chain.
- Return Result(5).
NOTE 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.