14 ECMAScript Language: Statements and Declarations
Syntax
14.1 Statement Semantics
14.1.1 Runtime Semantics: Evaluation
- Return
empty .
- Return ?
Evaluation ofFunctionDeclaration .
- Let newLabelSet be a new empty
List . - Return ?
LabelledEvaluation of thisBreakableStatement with argument newLabelSet.
14.2 Block
Syntax
14.2.1 Static Semantics: Early Errors
-
It is a Syntax Error if the
LexicallyDeclaredNames ofStatementList contains any duplicate entries, unless thehost is a web browser orotherwise supports Block-Level Function Declarations Web Legacy Compatibility Semantics , and both of the following conditions are true:IsStrict (this production) isfalse .- The duplicate entries are only bound by FunctionDeclarations.
-
It is a Syntax Error if any element of the
LexicallyDeclaredNames ofStatementList also occurs in theVarDeclaredNames ofStatementList .
14.2.2 Runtime Semantics: Evaluation
- Return
empty .
- Let oldEnv be the
running execution context 's LexicalEnvironment. - Let blockEnv be
NewDeclarativeEnvironment (oldEnv). - Perform
BlockDeclarationInstantiation (StatementList , blockEnv). - Set the
running execution context 's LexicalEnvironment to blockEnv. - Let blockValue be
Completion (Evaluation ofStatementList ). - Set the
running execution context 's LexicalEnvironment to oldEnv. - Return ? blockValue.
No matter how control leaves the
- Let sl be ?
Evaluation ofStatementList . - Let s be
Completion (Evaluation ofStatementListItem ). - Return ?
UpdateEmpty (s, sl).
The value of a eval function all return the value 1:
eval("1;;;;;")
eval("1;{}")
eval("1;var a;")
14.2.3 BlockDeclarationInstantiation ( code, envRecord )
The abstract operation BlockDeclarationInstantiation takes arguments code (a
When a
It performs the following steps when called:
- Let decls be the
LexicallyScopedDeclarations of code. - Let privateEnv be the
running execution context 's PrivateEnvironment. - For each element decl of decls, do
- For each element dn of the
BoundNames of decl, do- If
IsConstantDeclaration of decl istrue , then- Perform ! envRecord.CreateImmutableBinding(dn,
true ).
- Perform ! envRecord.CreateImmutableBinding(dn,
- Else,
- If the
host is a web browser orotherwise supports Block-Level Function Declarations Web Legacy Compatibility Semantics , then- If ! envRecord.HasBinding(dn) is
false , then- Perform ! envRecord.CreateMutableBinding(dn,
false ).
- Perform ! envRecord.CreateMutableBinding(dn,
- If ! envRecord.HasBinding(dn) is
- Else,
- Perform ! envRecord.CreateMutableBinding(dn,
false ).
- Perform ! envRecord.CreateMutableBinding(dn,
- If the
- If
- If decl is either a
FunctionDeclaration , aGeneratorDeclaration , anAsyncFunctionDeclaration , or anAsyncGeneratorDeclaration , then- Let func be the sole element of the
BoundNames of decl. - Let funcObj be
InstantiateFunctionObject of decl with arguments envRecord and privateEnv. - If the
host is a web browser orotherwise supports Block-Level Function Declarations Web Legacy Compatibility Semantics , then- If the binding for func in envRecord is an uninitialized binding, then
- Perform ! envRecord.InitializeBinding(func, funcObj).
- Else,
Assert : decl is aFunctionDeclaration .- Perform ! envRecord.SetMutableBinding(func, funcObj,
false ).
- If the binding for func in envRecord is an uninitialized binding, then
- Else,
- Perform ! envRecord.InitializeBinding(func, funcObj).
- Let func be the sole element of the
- For each element dn of the
- Return
unused .
14.3 Declarations and the Variable Statement
14.3.1 Let and Const Declarations
let and const declarations define variables that are scoped to the let declaration does not have an
Syntax
14.3.1.1 Static Semantics: Early Errors
-
It is a Syntax Error if the
BoundNames ofBindingList contains"let" . -
It is a Syntax Error if the
BoundNames ofBindingList contains any duplicate entries.
-
It is a Syntax Error if
Initializer is not present andIsConstantDeclaration of theLexicalDeclaration containing thisLexicalBinding istrue .
14.3.1.2 Runtime Semantics: Evaluation
- Perform ?
Evaluation ofBindingList . - Return
empty .
- Perform ?
Evaluation ofBindingList . - Return ?
Evaluation ofLexicalBinding .
- Let lhs be !
ResolveBinding (StringValue ofBindingIdentifier ). - Perform !
InitializeReferencedBinding (lhs,undefined ). - Return
empty .
A const declaration.
- Let bindingId be the
StringValue ofBindingIdentifier . - Let lhs be !
ResolveBinding (bindingId). - If
IsAnonymousFunctionDefinition (Initializer ) istrue , then- Let value be ?
NamedEvaluation ofInitializer with argument bindingId.
- Let value be ?
- Else,
- Let rhs be ?
Evaluation ofInitializer . - Let value be ?
GetValue (rhs).
- Let rhs be ?
- Perform !
InitializeReferencedBinding (lhs, value). - Return
empty .
- Let rhs be ?
Evaluation ofInitializer . - Let value be ?
GetValue (rhs). - Let envRecord be the
running execution context 's LexicalEnvironment. - Return ?
BindingInitialization ofBindingPattern with arguments value and envRecord.
14.3.2 Variable Statement
A var statement declares variables that are scoped to the
Syntax
14.3.2.1 Runtime Semantics: Evaluation
- Perform ?
Evaluation ofVariableDeclarationList . - Return
empty .
- Perform ?
Evaluation ofVariableDeclarationList . - Return ?
Evaluation ofVariableDeclaration .
- Return
empty .
- Let bindingId be the
StringValue ofBindingIdentifier . - Let lhs be ?
ResolveBinding (bindingId). - If
IsAnonymousFunctionDefinition (Initializer ) istrue , then- Let value be ?
NamedEvaluation ofInitializer with argument bindingId.
- Let value be ?
- Else,
- Let rhs be ?
Evaluation ofInitializer . - Let value be ?
GetValue (rhs).
- Let rhs be ?
- Perform ?
PutValue (lhs, value). - Return
empty .
If a
- Let rhs be ?
Evaluation ofInitializer . - Let rightValue be ?
GetValue (rhs). - Return ?
BindingInitialization ofBindingPattern with arguments rightValue andundefined .
14.3.3 Destructuring Binding Patterns
Syntax
14.3.3.1 Runtime Semantics: PropertyBindingInitialization
The
- Let boundNames be ? PropertyBindingInitialization of
BindingPropertyList with arguments value and envRecord. - Let nextNames be ? PropertyBindingInitialization of
BindingProperty with arguments value and envRecord. - Return the
list-concatenation of boundNames and nextNames.
- Let name be the sole element of the
BoundNames ofSingleNameBinding . - Perform ?
KeyedBindingInitialization ofSingleNameBinding with arguments value, envRecord, and name. - Return « name ».
- Let propertyKey be ?
Evaluation ofPropertyName . - Perform ?
KeyedBindingInitialization ofBindingElement with arguments value, envRecord, and propertyKey. - Return « propertyKey ».
14.3.3.2 Runtime Semantics: RestBindingInitialization
The
- Let lhs be ?
ResolveBinding (StringValue ofBindingIdentifier , envRecord). - Let restObj be
OrdinaryObjectCreate (%Object.prototype% ). - Perform ?
CopyDataProperties (restObj, value, excludedNames). - If envRecord is
undefined , return ?PutValue (lhs, restObj). - Return ?
InitializeReferencedBinding (lhs, restObj).
14.3.3.3 Runtime Semantics: KeyedBindingInitialization
The
When
It is defined piecewise over the following productions:
- Set value to ?
GetV (value, propertyName). - If
Initializer is present and value isundefined , then- Let defaultValue be ?
Evaluation ofInitializer . - Set value to ?
GetValue (defaultValue).
- Let defaultValue be ?
- Return ?
BindingInitialization ofBindingPattern with arguments value and envRecord.
- Let bindingId be the
StringValue ofBindingIdentifier . - Let lhs be ?
ResolveBinding (bindingId, envRecord). - Set value to ?
GetV (value, propertyName). - If
Initializer is present and value isundefined , then- If
IsAnonymousFunctionDefinition (Initializer ) istrue , then- Set value to ?
NamedEvaluation ofInitializer with argument bindingId.
- Set value to ?
- Else,
- Let defaultValue be ?
Evaluation ofInitializer . - Set value to ?
GetValue (defaultValue).
- Let defaultValue be ?
- If
- If envRecord is
undefined , return ?PutValue (lhs, value). - Return ?
InitializeReferencedBinding (lhs, value).
14.4 Empty Statement
Syntax
14.4.1 Runtime Semantics: Evaluation
- Return
empty .
14.5 Expression Statement
Syntax
An function or class async function because that would make it ambiguous with an let [ because that would make it ambiguous with a let
14.5.1 Runtime Semantics: Evaluation
- Let exprRef be ?
Evaluation ofExpression . - Return ?
GetValue (exprRef).
14.6 The if Statement
Syntax
else] resolves the classic “dangling else” problem in the usual way. That is, when the choice of associated if is otherwise ambiguous, the else is associated with the nearest (innermost) of the candidate ifs14.6.1 Static Semantics: Early Errors
-
It is a Syntax Error if
IsLabelledFunction (the firstStatement ) istrue . -
It is a Syntax Error if
IsLabelledFunction (the secondStatement ) istrue .
-
It is a Syntax Error if
IsLabelledFunction (Statement ) istrue .
It is only necessary to apply this rule if the extension specified in
14.6.2 Runtime Semantics: Evaluation
- Let exprRef be ?
Evaluation ofExpression . - Let exprValue be
ToBoolean (?GetValue (exprRef)). - If exprValue is
true , then- Let stmtCompletion be
Completion (Evaluation of the firstStatement ).
- Let stmtCompletion be
- Else,
- Let stmtCompletion be
Completion (Evaluation of the secondStatement ).
- Let stmtCompletion be
- Return ?
UpdateEmpty (stmtCompletion,undefined ).
- Let exprRef be ?
Evaluation ofExpression . - Let exprValue be
ToBoolean (?GetValue (exprRef)). - If exprValue is
false , returnundefined . - Let stmtCompletion be
Completion (Evaluation ofStatement ). - Return ?
UpdateEmpty (stmtCompletion,undefined ).
14.7 Iteration Statements
Syntax
14.7.1 Semantics
14.7.1.1 LoopContinues ( completion, labelSet )
The abstract operation LoopContinues takes arguments completion (a
- If completion is a
normal completion , returntrue . - If completion is not a
continue completion , returnfalse . - If completion.[[Target]] is
empty , returntrue . - If labelSet contains completion.[[Target]], return
true . - Return
false .
Within the
14.7.1.2 Runtime Semantics: LoopEvaluation
The
- Return ?
DoWhileLoopEvaluation ofDoWhileStatement with argument labelSet.
- Return ?
WhileLoopEvaluation ofWhileStatement with argument labelSet.
- Return ?
ForLoopEvaluation ofForStatement with argument labelSet.
- Return ?
ForInOfLoopEvaluation ofForInOfStatement with argument labelSet.
14.7.2 The do-while Statement
Syntax
14.7.2.1 Static Semantics: Early Errors
-
It is a Syntax Error if
IsLabelledFunction (Statement ) istrue .
It is only necessary to apply this rule if the extension specified in
14.7.2.2 Runtime Semantics: DoWhileLoopEvaluation
The
- Let iterationResult be
undefined . - Repeat,
- Let stmtResult be
Completion (Evaluation ofStatement ). - If
LoopContinues (stmtResult, labelSet) isfalse , return ?UpdateEmpty (stmtResult, iterationResult). - If stmtResult.[[Value]] is not
empty , set iterationResult to stmtResult.[[Value]]. - Let exprRef be ?
Evaluation ofExpression . - Let exprValue be ?
GetValue (exprRef). - If
ToBoolean (exprValue) isfalse , return iterationResult.
- Let stmtResult be
14.7.3 The while Statement
Syntax
14.7.3.1 Static Semantics: Early Errors
-
It is a Syntax Error if
IsLabelledFunction (Statement ) istrue .
It is only necessary to apply this rule if the extension specified in
14.7.3.2 Runtime Semantics: WhileLoopEvaluation
The
- Let iterationResult be
undefined . - Repeat,
- Let exprRef be ?
Evaluation ofExpression . - Let exprValue be ?
GetValue (exprRef). - If
ToBoolean (exprValue) isfalse , return iterationResult. - Let stmtResult be
Completion (Evaluation ofStatement ). - If
LoopContinues (stmtResult, labelSet) isfalse , return ?UpdateEmpty (stmtResult, iterationResult). - If stmtResult.[[Value]] is not
empty , set iterationResult to stmtResult.[[Value]].
- Let exprRef be ?
14.7.4 The for Statement
Syntax
14.7.4.1 Static Semantics: Early Errors
-
It is a Syntax Error if
IsLabelledFunction (Statement ) istrue .
It is only necessary to apply this rule if the extension specified in
-
It is a Syntax Error if any element of the
BoundNames ofLexicalDeclaration also occurs in theVarDeclaredNames ofStatement .
14.7.4.2 Runtime Semantics: ForLoopEvaluation
The
- If the first
Expression is present, then- Let exprRef be ?
Evaluation of the firstExpression . - Perform ?
GetValue (exprRef).
- Let exprRef be ?
- If the second
Expression is present, let test be the secondExpression ; else let test beempty . - If the third
Expression is present, let increment be the thirdExpression ; else let increment beempty . - Return ?
ForBodyEvaluation (test, increment,Statement , « », labelSet).
- Perform ?
Evaluation ofVariableDeclarationList . - If the first
Expression is present, let test be the firstExpression ; else let test beempty . - If the second
Expression is present, let increment be the secondExpression ; else let increment beempty . - Return ?
ForBodyEvaluation (test, increment,Statement , « », labelSet).
- Let oldEnv be the
running execution context 's LexicalEnvironment. - Let loopEnv be
NewDeclarativeEnvironment (oldEnv). - Let isConst be
IsConstantDeclaration ofLexicalDeclaration . - Let boundNames be the
BoundNames ofLexicalDeclaration . - For each element dn of boundNames, do
- If isConst is
true , then- Perform ! loopEnv.CreateImmutableBinding(dn,
true ).
- Perform ! loopEnv.CreateImmutableBinding(dn,
- Else,
- Perform ! loopEnv.CreateMutableBinding(dn,
false ).
- Perform ! loopEnv.CreateMutableBinding(dn,
- If isConst is
- Set the
running execution context 's LexicalEnvironment to loopEnv. - Let forDecl be
Completion (Evaluation ofLexicalDeclaration ). - If forDecl is an
abrupt completion , then- Set the
running execution context 's LexicalEnvironment to oldEnv. - Return ? forDecl.
- Set the
- If isConst is
false , let perIterationLets be boundNames; else let perIterationLets be a new emptyList . - If the first
Expression is present, let test be the firstExpression ; else let test beempty . - If the second
Expression is present, let increment be the secondExpression ; else let increment beempty . - Let bodyResult be
Completion (ForBodyEvaluation (test, increment,Statement , perIterationLets, labelSet)). - Set the
running execution context 's LexicalEnvironment to oldEnv. - Return ? bodyResult.
14.7.4.3 ForBodyEvaluation ( test, increment, stmt, perIterationBindings, labelSet )
The abstract operation ForBodyEvaluation takes arguments test (an
- Let iterationResult be
undefined . - Perform ?
CreatePerIterationEnvironment (perIterationBindings). - Repeat,
- If test is not
empty , then- Let testRef be ?
Evaluation of test. - Let testValue be ?
GetValue (testRef). - If
ToBoolean (testValue) isfalse , return iterationResult.
- Let testRef be ?
- Let result be
Completion (Evaluation of stmt). - If
LoopContinues (result, labelSet) isfalse , return ?UpdateEmpty (result, iterationResult). - If result.[[Value]] is not
empty , set iterationResult to result.[[Value]]. - Perform ?
CreatePerIterationEnvironment (perIterationBindings). - If increment is not
empty , then- Let incRef be ?
Evaluation of increment. - Perform ?
GetValue (incRef).
- Let incRef be ?
- If test is not
14.7.4.4 CreatePerIterationEnvironment ( perIterationBindings )
The abstract operation CreatePerIterationEnvironment takes argument perIterationBindings (a
- If perIterationBindings has any elements, then
- Let lastIterationEnv be the
running execution context 's LexicalEnvironment. - Let outer be lastIterationEnv.[[OuterEnv]].
Assert : outer is notnull .- Let thisIterationEnv be
NewDeclarativeEnvironment (outer). - For each element bn of perIterationBindings, do
- Perform ! thisIterationEnv.CreateMutableBinding(bn,
false ). - Let lastValue be ? lastIterationEnv.GetBindingValue(bn,
true ). - Perform ! thisIterationEnv.InitializeBinding(bn, lastValue).
- Perform ! thisIterationEnv.CreateMutableBinding(bn,
- Set the
running execution context 's LexicalEnvironment to thisIterationEnv.
- Let lastIterationEnv be the
- Return
unused .
14.7.5 The for-in, for-of, and for-await-of Statements
Syntax
This section is extended by Annex
14.7.5.1 Static Semantics: Early Errors
-
It is a Syntax Error if
IsLabelledFunction (Statement ) istrue .
It is only necessary to apply this rule if the extension specified in
-
If
LeftHandSideExpression is either anObjectLiteral or anArrayLiteral ,LeftHandSideExpression must cover anAssignmentPattern . -
If
LeftHandSideExpression is neither anObjectLiteral nor anArrayLiteral , it is a Syntax Error if theAssignmentTargetType ofLeftHandSideExpression isinvalid .
-
It is a Syntax Error if the
BoundNames ofForDeclaration contains"let" . -
It is a Syntax Error if any element of the
BoundNames ofForDeclaration also occurs in theVarDeclaredNames ofStatement . -
It is a Syntax Error if the
BoundNames ofForDeclaration contains any duplicate entries.
14.7.5.2 Static Semantics: IsDestructuring
The
- If
PrimaryExpression is either anObjectLiteral or anArrayLiteral , returntrue . - Return
false .
- Return
false .
- Return IsDestructuring of
ForBinding .
- Return
false .
- Return
true .
This section is extended by Annex
14.7.5.3 Runtime Semantics: ForDeclarationBindingInitialization
The
- Return ?
BindingInitialization ofForBinding with arguments value and envRecord.
14.7.5.4 Runtime Semantics: ForDeclarationBindingInstantiation
The
- For each element name of the
BoundNames ofForBinding , do- If
IsConstantDeclaration ofLetOrConst istrue , then- Perform ! envRecord.CreateImmutableBinding(name,
true ).
- Perform ! envRecord.CreateImmutableBinding(name,
- Else,
- Perform ! envRecord.CreateMutableBinding(name,
false ).
- Perform ! envRecord.CreateMutableBinding(name,
- If
- Return
unused .
14.7.5.5 Runtime Semantics: ForInOfLoopEvaluation
The
- Let keyResult be ? ForIn/OfHeadEvaluation(« »,
Expression ,enumerate ). - Return ? ForIn/OfBodyEvaluation(
LeftHandSideExpression ,Statement , keyResult,enumerate ,assignment , labelSet).
- Let keyResult be ? ForIn/OfHeadEvaluation(« »,
Expression ,enumerate ). - Return ? ForIn/OfBodyEvaluation(
ForBinding ,Statement , keyResult,enumerate ,var-binding , labelSet).
- Let keyResult be ? ForIn/OfHeadEvaluation(
BoundNames ofForDeclaration ,Expression ,enumerate ). - Return ? ForIn/OfBodyEvaluation(
ForDeclaration ,Statement , keyResult,enumerate ,lexical-binding , labelSet).
- Let keyResult be ? ForIn/OfHeadEvaluation(« »,
AssignmentExpression ,iterate ). - Return ? ForIn/OfBodyEvaluation(
LeftHandSideExpression ,Statement , keyResult,iterate ,assignment , labelSet).
- Let keyResult be ? ForIn/OfHeadEvaluation(« »,
AssignmentExpression ,iterate ). - Return ? ForIn/OfBodyEvaluation(
ForBinding ,Statement , keyResult,iterate ,var-binding , labelSet).
- Let keyResult be ? ForIn/OfHeadEvaluation(
BoundNames ofForDeclaration ,AssignmentExpression ,iterate ). - Return ? ForIn/OfBodyEvaluation(
ForDeclaration ,Statement , keyResult,iterate ,lexical-binding , labelSet).
- Let keyResult be ? ForIn/OfHeadEvaluation(« »,
AssignmentExpression ,async-iterate ). - Return ? ForIn/OfBodyEvaluation(
LeftHandSideExpression ,Statement , keyResult,iterate ,assignment , labelSet,async ).
- Let keyResult be ? ForIn/OfHeadEvaluation(« »,
AssignmentExpression ,async-iterate ). - Return ? ForIn/OfBodyEvaluation(
ForBinding ,Statement , keyResult,iterate ,var-binding , labelSet,async ).
- Let keyResult be ? ForIn/OfHeadEvaluation(
BoundNames ofForDeclaration ,AssignmentExpression ,async-iterate ). - Return ? ForIn/OfBodyEvaluation(
ForDeclaration ,Statement , keyResult,iterate ,lexical-binding , labelSet,async ).
This section is extended by Annex
14.7.5.6 ForIn/OfHeadEvaluation ( uninitializedBoundNames, expr, iterationKind )
The abstract operation ForIn/OfHeadEvaluation takes arguments uninitializedBoundNames (a
- Let oldEnv be the
running execution context 's LexicalEnvironment. - If uninitializedBoundNames is not empty, then
Assert : uninitializedBoundNames has no duplicate entries.- Let newEnv be
NewDeclarativeEnvironment (oldEnv). - For each String name of uninitializedBoundNames, do
- Perform ! newEnv.CreateMutableBinding(name,
false ).
- Perform ! newEnv.CreateMutableBinding(name,
- Set the
running execution context 's LexicalEnvironment to newEnv.
- Let exprRef be
Completion (Evaluation of expr). - Set the
running execution context 's LexicalEnvironment to oldEnv. - Let exprValue be ?
GetValue (? exprRef). - If iterationKind is
enumerate , then- If exprValue is either
undefined ornull , then- Return
Completion Record { [[Type]]:break , [[Value]]:empty , [[Target]]:empty }.
- Return
- Let obj be !
ToObject (exprValue). - Let iterator be
EnumerateObjectProperties (obj). - Let nextMethod be !
GetV (iterator,"next" ). - Return the
Iterator Record { [[Iterator]]: iterator, [[NextMethod]]: nextMethod, [[Done]]:false }.
- If exprValue is either
Assert : iterationKind is eitheriterate orasync-iterate .- If iterationKind is
async-iterate , let iteratorKind beasync . - Else, let iteratorKind be
sync . - Return ?
GetIterator (exprValue, iteratorKind).
14.7.5.7 ForIn/OfBodyEvaluation ( lhs, stmt, iteratorRecord, iterationKind, lhsKind, labelSet [ , iteratorKind ] )
The abstract operation ForIn/OfBodyEvaluation takes arguments lhs (a
- If iteratorKind is not present, set iteratorKind to
sync . - Let oldEnv be the
running execution context 's LexicalEnvironment. - Let iterationResult be
undefined . - Let destructuring be
IsDestructuring of lhs. - If destructuring is
true and lhsKind isassignment , thenAssert : lhs is aLeftHandSideExpression .- Let assignmentPattern be the
AssignmentPattern that iscovered by lhs.
- Repeat,
- Let nextResult be ?
Call (iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]]). - If iteratorKind is
async , set nextResult to ?Await (nextResult). - If nextResult
is not an Object , throw aTypeError exception. - Let done be ?
IteratorComplete (nextResult). - If done is
true , return iterationResult. - Let nextValue be ?
IteratorValue (nextResult). - If lhsKind is either
assignment orvar-binding , then- If destructuring is
true , then- If lhsKind is
assignment , then- Let status be
Completion (DestructuringAssignmentEvaluation of assignmentPattern with argument nextValue).
- Let status be
- Else,
Assert : lhsKind isvar-binding .Assert : lhs is aForBinding .- Let status be
Completion (BindingInitialization of lhs with arguments nextValue andundefined ).
- If lhsKind is
- Else,
- Let lhsRef be
Completion (Evaluation of lhs). (It may be evaluated repeatedly.) - If lhsKind is
assignment and theAssignmentTargetType of lhs isweb-compat , throw aReferenceError exception. - If lhsRef is an
abrupt completion , then- Let status be lhsRef.
- Else,
- Let status be
Completion (PutValue (lhsRef.[[Value]], nextValue)).
- Let status be
- Let lhsRef be
- If destructuring is
- Else,
Assert : lhsKind islexical-binding .Assert : lhs is aForDeclaration .- Let iterationEnv be
NewDeclarativeEnvironment (oldEnv). - Perform
ForDeclarationBindingInstantiation of lhs with argument iterationEnv. - Set the
running execution context 's LexicalEnvironment to iterationEnv. - If destructuring is
true , then- Let status be
Completion (ForDeclarationBindingInitialization of lhs with arguments nextValue and iterationEnv).
- Let status be
- Else,
Assert : lhs binds a single name.- Let lhsName be the sole element of the
BoundNames of lhs. - Let lhsRef be !
ResolveBinding (lhsName). - Let status be
Completion (InitializeReferencedBinding (lhsRef, nextValue)).
- If status is an
abrupt completion , then- Set the
running execution context 's LexicalEnvironment to oldEnv. - If iterationKind is
enumerate , return ? status. Assert : iterationKind isiterate .- If iteratorKind is
async , return ?AsyncIteratorClose (iteratorRecord, status). - Return ?
IteratorClose (iteratorRecord, status).
- Set the
- Let result be
Completion (Evaluation of stmt). - Set the
running execution context 's LexicalEnvironment to oldEnv. - If
LoopContinues (result, labelSet) isfalse , then- Set status to
Completion (UpdateEmpty (result, iterationResult)). - If iterationKind is
enumerate , return ? status. Assert : iterationKind isiterate .- If iteratorKind is
async , return ?AsyncIteratorClose (iteratorRecord, status). - Return ?
IteratorClose (iteratorRecord, status).
- Set status to
- If result.[[Value]] is not
empty , set iterationResult to result.[[Value]].
- Let nextResult be ?
14.7.5.8 Runtime Semantics: Evaluation
- Let bindingId be the
StringValue ofBindingIdentifier . - Return ?
ResolveBinding (bindingId).
14.7.5.9 EnumerateObjectProperties ( obj )
The abstract operation EnumerateObjectProperties takes argument obj (an Object) and returns an
- Return an
iterator object whosenextmethod iterates over all the String-valued keys of enumerable properties of obj. Theiterator object is never directly accessible to ECMAScript code. The mechanics and order of enumerating the properties is not specified but must conform to the rules specified below.
The throw and return methods are next method processes object properties to determine whether the next method is ignored. If new properties are added to the target object during enumeration, the newly added properties are not guaranteed to be processed in the active enumeration. A next method at most once in any enumeration.
Enumerating the properties of the target 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 processed if it has the same name as a property that has already been processed by the next method. The values of [[Enumerable]] attributes are not considered when determining if a property of a prototype object has already been processed. The enumerable
In addition, if neither obj nor any object in its prototype chain is a
- the value of the [[Prototype]] internal slot of obj or an object in its prototype chain changes,
- a property is removed from obj or an object in its prototype chain,
- a property is added to an object in obj's prototype chain, or
- the value of the [[Enumerable]] attribute of a property of obj or an object in its prototype chain changes.
ECMAScript implementations are not required to implement the algorithm in
The following is an informative definition of an ECMAScript generator function that conforms to these rules:
function* EnumerateObjectProperties(obj) {
const visited = new Set();
for (const key of Reflect.ownKeys(obj)) {
if (typeof key === "symbol") continue;
const desc = Reflect.getOwnPropertyDescriptor(obj, key);
if (desc) {
visited.add(key);
if (desc.enumerable) yield key;
}
}
const proto = Reflect.getPrototypeOf(obj);
if (proto === null) return;
for (const protoKey of EnumerateObjectProperties(proto)) {
if (!visited.has(protoKey)) yield protoKey;
}
}
14.7.5.10 For-In Iterator Objects
A For-In Iterator is an object that represents a specific iteration over some specific object. For-In Iterator objects are never directly accessible to ECMAScript code; they exist solely to illustrate the behaviour of
14.7.5.10.1 CreateForInIterator ( obj )
The abstract operation CreateForInIterator takes argument obj (an Object) and returns a
- Let iterator be
OrdinaryObjectCreate (%ForInIteratorPrototype% , « [[Object]], [[ObjectWasVisited]], [[VisitedKeys]], [[RemainingKeys]] »). - Set iterator.[[Object]] to obj.
- Set iterator.[[ObjectWasVisited]] to
false . - Set iterator.[[VisitedKeys]] to a new empty
List . - Set iterator.[[RemainingKeys]] to a new empty
List . - Return iterator.
14.7.5.10.2 The %ForInIteratorPrototype% Object
The
- has properties that are inherited by all
For-In Iterator objects . - is an
ordinary object . - has a [[Prototype]] internal slot whose value is
%Iterator.prototype% . - is never directly accessible to ECMAScript code.
- has the following properties:
14.7.5.10.2.1 %ForInIteratorPrototype% .next ( )
- Let iterator be the
this value. Assert : iteratoris an Object .Assert : iterator has all of the internal slots of aFor-In Iterator instance (14.7.5.10.3 ).- Let obj be iterator.[[Object]].
- Repeat,
- If iterator.[[ObjectWasVisited]] is
false , then- Let keys be ?
obj.[[OwnPropertyKeys]]() . - For each element key of keys, do
- If key
is a String , then- Append key to iterator.[[RemainingKeys]].
- If key
- Set iterator.[[ObjectWasVisited]] to
true .
- Let keys be ?
- Repeat, while iterator.[[RemainingKeys]] is not empty,
- Let key be the first element of iterator.[[RemainingKeys]].
- Remove the first element from iterator.[[RemainingKeys]].
- If iterator.[[VisitedKeys]] does not contain key, then
- Let propertyDesc be ?
obj.[[GetOwnProperty]](key) . - If propertyDesc is not
undefined , then- Append key to iterator.[[VisitedKeys]].
- If propertyDesc.[[Enumerable]] is
true , returnCreateIteratorResultObject (key,false ).
- Let propertyDesc be ?
- Set obj to ?
obj.[[GetPrototypeOf]]() . - Set iterator.[[Object]] to obj.
- Set iterator.[[ObjectWasVisited]] to
false . - If obj is
null , returnCreateIteratorResultObject (undefined ,true ).
- If iterator.[[ObjectWasVisited]] is
14.7.5.10.3 Properties of For-In Iterator Instances
| Internal Slot | Type | Description |
|---|---|---|
| [[Object]] | an Object | The Object value whose properties are being iterated. |
| [[ObjectWasVisited]] | a Boolean |
|
| [[VisitedKeys]] |
a |
The values that have been emitted by this |
| [[RemainingKeys]] |
a |
The values remaining to be emitted for the current object, before iterating the properties of its prototype (if its prototype is not |
14.8 The continue Statement
Syntax
14.8.1 Static Semantics: Early Errors
-
It is a Syntax Error if this
ContinueStatement is not nested, directly or indirectly (but not crossing function orstaticinitialization block boundaries), within anIterationStatement .
14.8.2 Runtime Semantics: Evaluation
- Return
Completion Record { [[Type]]:continue , [[Value]]:empty , [[Target]]:empty }.
- Let label be the
StringValue ofLabelIdentifier . - Return
Completion Record { [[Type]]:continue , [[Value]]:empty , [[Target]]: label }.
14.9 The break Statement
Syntax
14.9.1 Static Semantics: Early Errors
-
It is a Syntax Error if this
BreakStatement is not nested, directly or indirectly (but not crossing function orstaticinitialization block boundaries), within anIterationStatement or aSwitchStatement .
14.9.2 Runtime Semantics: Evaluation
- Return
Completion Record { [[Type]]:break , [[Value]]:empty , [[Target]]:empty }.
- Let label be the
StringValue ofLabelIdentifier . - Return
Completion Record { [[Type]]:break , [[Value]]:empty , [[Target]]: label }.
14.10 The return Statement
Syntax
A return statement causes a function to cease execution and, in most cases, returns a value to the caller. If return statement may not actually return a value to the caller depending on surrounding context. For example, in a try block, a return statement's finally block.
14.10.1 Runtime Semantics: Evaluation
- Return
ReturnCompletion (undefined ).
- Let exprRef be ?
Evaluation ofExpression . - Let exprValue be ?
GetValue (exprRef). - If
GetGeneratorKind () isasync , set exprValue to ?Await (exprValue). - Return
ReturnCompletion (exprValue).
14.11 The with Statement
Use of the with statement is discouraged in new ECMAScript code. Consider alternatives that are permitted in both
Syntax
The with statement adds an
14.11.1 Static Semantics: Early Errors
-
It is a Syntax Error if
IsStrict (this production) istrue . -
It is a Syntax Error if
IsLabelledFunction (Statement ) istrue .
It is only necessary to apply the second rule if the extension specified in
14.11.2 Runtime Semantics: Evaluation
- Let value be ?
Evaluation ofExpression . - Let obj be ?
ToObject (?GetValue (value)). - Let oldEnv be the
running execution context 's LexicalEnvironment. - Let newEnv be
NewObjectEnvironment (obj,true , oldEnv). - Set the
running execution context 's LexicalEnvironment to newEnv. - Let stmtCompletion be
Completion (Evaluation ofStatement ). - Set the
running execution context 's LexicalEnvironment to oldEnv. - Return ?
UpdateEmpty (stmtCompletion,undefined ).
No matter how control leaves the embedded
14.12 The switch Statement
Syntax
14.12.1 Static Semantics: Early Errors
-
It is a Syntax Error if the
LexicallyDeclaredNames ofCaseBlock contains any duplicate entries, unless thehost is a web browser orotherwise supports Block-Level Function Declarations Web Legacy Compatibility Semantics , and both of the following conditions are true:IsStrict (this production) isfalse .- The duplicate entries are only bound by FunctionDeclarations.
-
It is a Syntax Error if any element of the
LexicallyDeclaredNames ofCaseBlock also occurs in theVarDeclaredNames ofCaseBlock .
14.12.2 Runtime Semantics: CaseBlockEvaluation
The
- Return
undefined .
- Let resultValue be
undefined . - Let caseClauses be the
List ofCaseClause items inCaseClauses , in source text order. - Let found be
false . - For each
CaseClause clause of caseClauses, do- If found is
false , then- Set found to ?
CaseClauseIsSelected (clause, input).
- Set found to ?
- If found is
true , then- Let completion be
Completion (Evaluation of clause). - If completion.[[Value]] is not
empty , set resultValue to completion.[[Value]]. - If completion is an
abrupt completion , return ?UpdateEmpty (completion, resultValue).
- Let completion be
- If found is
- Return resultValue.
- Let resultValue be
undefined . - If the first
CaseClauses is present, then- Let caseClauses be the
List ofCaseClause items in the firstCaseClauses , in source text order.
- Let caseClauses be the
- Else,
- Let caseClauses be a new empty
List .
- Let caseClauses be a new empty
- Let found be
false . - For each
CaseClause clause of caseClauses, do- If found is
false , then- Set found to ?
CaseClauseIsSelected (clause, input).
- Set found to ?
- If found is
true , then- Let completion be
Completion (Evaluation of clause). - If completion.[[Value]] is not
empty , set resultValue to completion.[[Value]]. - If completion is an
abrupt completion , return ?UpdateEmpty (completion, resultValue).
- Let completion be
- If found is
- Let foundInB be
false . - If the second
CaseClauses is present, then- Let secondCaseClauses be the
List ofCaseClause items in the secondCaseClauses , in source text order.
- Let secondCaseClauses be the
- Else,
- Let secondCaseClauses be a new empty
List .
- Let secondCaseClauses be a new empty
- If found is
false , then- For each
CaseClause clause of secondCaseClauses, do- If foundInB is
false , then- Set foundInB to ?
CaseClauseIsSelected (clause, input).
- Set foundInB to ?
- If foundInB is
true , then- Let completion be
Completion (Evaluation ofCaseClause clause). - If completion.[[Value]] is not
empty , set resultValue to completion.[[Value]]. - If completion is an
abrupt completion , return ?UpdateEmpty (completion, resultValue).
- Let completion be
- If foundInB is
- For each
- If foundInB is
true , return resultValue. - Let defaultR be
Completion (Evaluation ofDefaultClause ). - If defaultR.[[Value]] is not
empty , set resultValue to defaultR.[[Value]]. - If defaultR is an
abrupt completion , return ?UpdateEmpty (defaultR, resultValue). NOTE : The following is another complete iteration of the secondCaseClauses .- For each
CaseClause clause of secondCaseClauses, do- Let completion be
Completion (Evaluation ofCaseClause clause). - If completion.[[Value]] is not
empty , set resultValue to completion.[[Value]]. - If completion is an
abrupt completion , return ?UpdateEmpty (completion, resultValue).
- Let completion be
- Return resultValue.
14.12.3 CaseClauseIsSelected ( caseClauseNode, input )
The abstract operation CaseClauseIsSelected takes arguments caseClauseNode (a
Assert : caseClauseNode is an instance of the production .CaseClause : case Expression : StatementList opt - Let exprRef be ?
Evaluation of theExpression of caseClauseNode. - Let clauseSelector be ?
GetValue (exprRef). - Return
IsStrictlyEqual (input, clauseSelector).
This operation does not execute caseClauseNode's
14.12.4 Runtime Semantics: Evaluation
- Let exprRef be ?
Evaluation ofExpression . - Let switchValue be ?
GetValue (exprRef). - Let oldEnv be the
running execution context 's LexicalEnvironment. - Let blockEnv be
NewDeclarativeEnvironment (oldEnv). - Perform
BlockDeclarationInstantiation (CaseBlock , blockEnv). - Set the
running execution context 's LexicalEnvironment to blockEnv. - Let blockResult be
Completion (CaseBlockEvaluation ofCaseBlock with argument switchValue). - Set the
running execution context 's LexicalEnvironment to oldEnv. - Return blockResult.
No matter how control leaves the
- Return
empty .
- Return ?
Evaluation ofStatementList .
- Return
empty .
- Return ?
Evaluation ofStatementList .
14.13 Labelled Statements
Syntax
A break and continue statements. ECMAScript has no goto statement. A
14.13.1 Static Semantics: Early Errors
-
It is a Syntax Error if any source text is matched by this production, unless that source text is
non-strict code and thehost is a web browser orotherwise supports Labelled Function Declarations .
14.13.2 Static Semantics: IsLabelledFunction ( stmt )
The abstract operation IsLabelledFunction takes argument stmt (a
- If stmt is not a
LabelledStatement , returnfalse . - Let item be the
LabelledItem of stmt. - If item is
, returnLabelledItem : FunctionDeclaration true . - Let subStmt be the
Statement of item. - Return IsLabelledFunction(subStmt).
14.13.3 Runtime Semantics: Evaluation
- Return ?
LabelledEvaluation of thisLabelledStatement with argument « ».
14.13.4 Runtime Semantics: LabelledEvaluation
The
- Let stmtResult be
Completion (LoopEvaluation ofIterationStatement with argument labelSet). - If stmtResult is a
break completion , then- If stmtResult.[[Target]] is
empty , then- If stmtResult.[[Value]] is
empty , set stmtResult toNormalCompletion (undefined ). - Else, set stmtResult to
NormalCompletion (stmtResult.[[Value]]).
- If stmtResult.[[Value]] is
- If stmtResult.[[Target]] is
- Return ? stmtResult.
- Let stmtResult be
Completion (Evaluation ofSwitchStatement ). - If stmtResult is a
break completion , then- If stmtResult.[[Target]] is
empty , then- If stmtResult.[[Value]] is
empty , set stmtResult toNormalCompletion (undefined ). - Else, set stmtResult to
NormalCompletion (stmtResult.[[Value]]).
- If stmtResult.[[Value]] is
- If stmtResult.[[Target]] is
- Return ? stmtResult.
A
- Let label be the
StringValue ofLabelIdentifier . - Let newLabelSet be the
list-concatenation of labelSet and « label ». - Let stmtResult be
Completion (LabelledEvaluation ofLabelledItem with argument newLabelSet). - If stmtResult is a
break completion and stmtResult.[[Target]] is label, then- Set stmtResult to
NormalCompletion (stmtResult.[[Value]]).
- Set stmtResult to
- Return ? stmtResult.
- Return ?
Evaluation ofFunctionDeclaration .
- Return ?
Evaluation ofStatement .
The only two productions of
14.14 The throw Statement
Syntax
14.14.1 Runtime Semantics: Evaluation
- Let exprRef be ?
Evaluation ofExpression . - Let exprValue be ?
GetValue (exprRef). - Throw exprValue.
14.15 The try Statement
Syntax
The try statement encloses a block of code in which an exceptional condition can occur, such as a runtime error or a throw statement. The catch clause provides the exception-handling code. When a catch clause catches an exception, its
14.15.1 Static Semantics: Early Errors
-
It is a Syntax Error if the
BoundNames ofCatchParameter contains any duplicate elements. -
It is a Syntax Error if any element of the
BoundNames ofCatchParameter also occurs in theLexicallyDeclaredNames ofBlock . -
It is a Syntax Error if any element of the
BoundNames ofCatchParameter also occurs in theVarDeclaredNames ofBlock , unlessCatchParameter is and theCatchParameter : BindingIdentifier host is a web browser orotherwise supports VariableStatements in Catch Blocks .
14.15.2 Runtime Semantics: CatchClauseEvaluation
The
- Let oldEnv be the
running execution context 's LexicalEnvironment. - Let catchEnv be
NewDeclarativeEnvironment (oldEnv). - For each element argName of the
BoundNames ofCatchParameter , do- Perform ! catchEnv.CreateMutableBinding(argName,
false ).
- Perform ! catchEnv.CreateMutableBinding(argName,
- Set the
running execution context 's LexicalEnvironment to catchEnv. - Let status be
Completion (BindingInitialization ofCatchParameter with arguments thrownValue and catchEnv). - If status is an
abrupt completion , then- Set the
running execution context 's LexicalEnvironment to oldEnv. - Return ? status.
- Set the
- Let blockCompletion be
Completion (Evaluation ofBlock ). - Set the
running execution context 's LexicalEnvironment to oldEnv. - Return ? blockCompletion.
- Return ?
Evaluation ofBlock .
No matter how control leaves the
14.15.3 Runtime Semantics: Evaluation
- Let blockResult be
Completion (Evaluation ofBlock ). - If blockResult is a
throw completion , let catchResult beCompletion (CatchClauseEvaluation ofCatch with argument blockResult.[[Value]]). - Else, let catchResult be blockResult.
- Return ?
UpdateEmpty (catchResult,undefined ).
- Let blockResult be
Completion (Evaluation ofBlock ). - Let finallyResult be
Completion (Evaluation ofFinally ). - If finallyResult is a
normal completion , set finallyResult to blockResult. - Return ?
UpdateEmpty (finallyResult,undefined ).
- Let blockResult be
Completion (Evaluation ofBlock ). - If blockResult is a
throw completion , let catchResult beCompletion (CatchClauseEvaluation ofCatch with argument blockResult.[[Value]]). - Else, let catchResult be blockResult.
- Let finallyResult be
Completion (Evaluation ofFinally ). - If finallyResult is a
normal completion , set finallyResult to catchResult. - Return ?
UpdateEmpty (finallyResult,undefined ).
14.16 The debugger Statement
Syntax
14.16.1 Runtime Semantics: Evaluation
Evaluating a
- If an
implementation-defined debugging facility is available and enabled, then- Perform an
implementation-defined debugging action. - Return a new
implementation-defined Completion Record .
- Perform an
- Return
empty .