Scope Summary

                                                       Summary of Scope
       As a beginner in Javascript, it is easy to make mistakes if the scope of variables is unclear, so I choose to summarize the scope of variables.
       First of all, all the code enclosed in {} can be called a code block. Each block of code has its own scope. The scope determines whether the variable can be accessed (whether reading the variable or modifying the variable). The code area between {} is the scope of this code block. Variables declared within the scope of a code block can only be accessed within the scope of that code block.
let x=5;
console.log(x); //Can access x, the result is 5
console.log(x); //unreachable (x is not in the scope of the code block, so cannot be accessed)

        If a new code block is written in a code block, the latter is the child scope of the former code block, and the former is the parent scope of the latter. The child scope can access the variables in the parent scope, but the parent scope cannot access the variables of the child scope (when the child scope has the variable name of the variable scope of the parent scope, only the child scope can be accessed in the child scope. Variables). In the top-level scope that is not enclosed by {} (ie, the top-level scope), the declared variable can also be accessed anywhere.
        Functions can declare variables in the parameter list, the values ​​of the parameters are passed in when they are called, and variables declared in the parent scope can also be accessed in the function.

Guess you like