What is block-level scope?
Definition:
Block-level scopes have been added in ES6. Block scopes are included by {}. The {} in the if and for statements are also block scopes.
Why do YOU need block-level scopes?
Prior to ES6, functions could only be declared in global and function scopes, not block-level scopes. The lack of block-level scope makes many scenarios irrational. 1. Memory variables may overwrite outer variables. 2Copy the code
Block-level scopes in ES6:
The outer scope cannot read variables of the inner scope. 2. The inner scope can define variables of the same name of the outer scopeCopy the code
Es6 variable declaration
Var: let: define a local variable instead of var const: define a constant.Copy the code
Var, let, const
1. Variables defined by var have no concept of blocks and can be accessed across blocks rather than functions. 2. Variables defined by let can only be accessed within the block scope, not across blocks or functions. 3. Const is used to define constants, which must be initialized (that is, assigned), accessed only in the block scope, and cannot be modified.Copy the code
Let and const
In contrast, variables declared with var can be repeatedly declared without block-level scope. Can't restrictCopy the code