consider the following code
var myVar = 'Hola';
{
let myVar;
myVar = 'Hello'
}
on line 4(myVar = 'Hello') we are using the Assignment operator
Now when i was looking at ecma262 in Assignment Operators Evaluation
it says that the left side in assignment operators is LeftHandSideExpression and the right side is AssignmentExpression
in other words it looks like that
LeftHandSideExpression = AssignmentExpression
can anyone explain to me how myVar is going to get evaluated ? if it's supposed to be LeftHandSideExpression ?
The main piece to understand is that
evaluatein this context means we're running theseRuntime Semantics: Evaluationsections for the various grammar pieces in the language. In the case ofif we look at 13.15.2 Runtime Semantics: Evaluation for the evaluation of
AssignmentExpressionthe linewill drill down through the various steps until you eventually end up running 13.1.3 Runtime Semantics: Evaluation which defines the evaluation behavior of
myVar.The key thing if you step through this is that
lrefdoes not evaluation to a JS value, it evaluates to aReference Recordtype, which isn't a value JS code is aware of, but represents the concept of a location where a value can be assigned. In the case of your snippet, it's basically a reference to the scope where the variable list, and the name of the variable, so when assignment is performed later, it is fully resolves later.This is behavior of evaluating the left side first more important for examples like this:
because evaluating the left means that
foo()runs beforeval(), which is what you'd expect. In this casefoo()runs and then we get aReference Recordwith the return value offoo()as the target of the assignment, andbaras the property name to assign.Going back to 13.15.2 Runtime Semantics: Evaluation, we get to
which is where the final assignment happens.
rvalis the"Hello"string itself since it has been evaluated. If you take a look at 6.2.4.6 PutValue ( V, W ), you can probably guess that the 3rd section is what we end up with for a variable assignmentso in you're case it's