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
- Let next be the result of evaluating
BindingList . ReturnIfAbrupt (next).- Return
NormalCompletion (empty ).
- Let next be the result of evaluating
BindingList . ReturnIfAbrupt (next).- Return the result of evaluating
LexicalBinding .
- Let lhs be
ResolveBinding (StringValue ofBindingIdentifier ). - Return
InitializeReferencedBinding (lhs,undefined ).
A const
declaration.
- Let bindingId be
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 the result of evaluating
Initializer . - Let value be ?
GetValue (rhs).
- Let rhs be the result of evaluating
- Return
InitializeReferencedBinding (lhs, value).
- Let rhs be the result of evaluating
Initializer . - Let value be ?
GetValue (rhs). - Let env be the
running execution context 's LexicalEnvironment. - Return the result of performing
BindingInitialization forBindingPattern using value and env as the arguments.
14.3.2 Variable Statement
A var
statement declares variables that are scoped to the
Syntax
14.3.2.1 Runtime Semantics: Evaluation
- Let next be the result of evaluating
VariableDeclarationList . ReturnIfAbrupt (next).- Return
NormalCompletion (empty ).
- Let next be the result of evaluating
VariableDeclarationList . ReturnIfAbrupt (next).- Return the result of evaluating
VariableDeclaration .
- Return
NormalCompletion (empty ).
- Let bindingId be
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 the result of evaluating
Initializer . - Let value be ?
GetValue (rhs).
- Let rhs be the result of evaluating
- Return ?
PutValue (lhs, value).
If a
- Let rhs be the result of evaluating
Initializer . - Let rval be ?
GetValue (rhs). - Return the result of performing
BindingInitialization forBindingPattern passing rval andundefined as arguments.
14.3.3 Destructuring Binding Patterns
Syntax
14.3.3.1 Runtime Semantics: PropertyBindingInitialization
With parameters value and environment.
- Let boundNames be ?
PropertyBindingInitialization ofBindingPropertyList with arguments value and environment. - Let nextNames be ?
PropertyBindingInitialization ofBindingProperty with arguments value and environment. - Append each item in nextNames to the end of boundNames.
- Return boundNames.
- Let name be the string that is the only element of
BoundNames ofSingleNameBinding . - Perform ?
KeyedBindingInitialization forSingleNameBinding using value, environment, and name as the arguments. - Return a
List whose sole element is name.
- Let P be the result of evaluating
PropertyName . ReturnIfAbrupt (P).- Perform ?
KeyedBindingInitialization ofBindingElement with value, environment, and P as the arguments. - Return a
List whose sole element is P.
14.3.3.2 Runtime Semantics: RestBindingInitialization
With parameters value, environment, and excludedNames.
- Let lhs be ?
ResolveBinding (StringValue ofBindingIdentifier , environment). - Let restObj be !
OrdinaryObjectCreate (%Object.prototype% ). - Perform ?
CopyDataProperties (restObj, value, excludedNames). - If environment is
undefined , returnPutValue (lhs, restObj). - Return
InitializeReferencedBinding (lhs, restObj).
14.3.3.3 Runtime Semantics: KeyedBindingInitialization
With parameters value, environment, and propertyName.
When
- Let v be ?
GetV (value, propertyName). - If
Initializer is present and v isundefined , then- Let defaultValue be the result of evaluating
Initializer . - Set v to ?
GetValue (defaultValue).
- Let defaultValue be the result of evaluating
- Return the result of performing
BindingInitialization forBindingPattern passing v and environment as arguments.
- Let bindingId be
StringValue ofBindingIdentifier . - Let lhs be ?
ResolveBinding (bindingId, environment). - Let v be ?
GetV (value, propertyName). - If
Initializer is present and v isundefined , then- If
IsAnonymousFunctionDefinition (Initializer ) istrue , then- Set v to the result of performing
NamedEvaluation forInitializer with argument bindingId.
- Set v to the result of performing
- Else,
- Let defaultValue be the result of evaluating
Initializer . - Set v to ?
GetValue (defaultValue).
- Let defaultValue be the result of evaluating
- If
- If environment is
undefined , return ?PutValue (lhs, v). - Return
InitializeReferencedBinding (lhs, v).