Scopes are a set of rules that determine where and how to find variables, functions, and so on (identifiers). If the purpose of the lookup is to assign a value to a variable, the LHS query is used, and if the purpose is to get the value of the variable, the RHS query is used.
The assignment operator causes the LHS query to be queried. = operator or an operation that passes in a parameter when the function is called will result in an assignment of the associated scope.
The JavaScript engine first compiles the code before it executes, and in this process a declaration like var a = 2 is decomposed into two separate steps:
- First, Var a declares a new variable in its scope. This is done at the very beginning, that is, before the code executes.
- Next, a = 2 queries (LHS query) variable A and assigns it a value.
LHS and RHS queries start in the current execution scope, and if necessary (that is, they do not find the required identifier), they continue to look for the target identifier to the ancestor scope, so that each ascent to the first-level scope (one floor), and finally to the global scope (top level), is stopped, whether found or not found.
An unsuccessful RHS reference causes the Referenceerror exception to be thrown. An unsuccessful LHS reference causes a global variable to be automatically created implicitly (in a non-strict mode) that uses the target referenced by LHS as an identifier, or throws a Referenceerror exception (in strict mode).
You don't know what the javascript-scope is