About var let const ~

Global scope: var block-level scope: let const

First, the global scope

1. The outermost layer functions and variables in the outermost layer has a function of global scope defined

2. All the end of the definition of direct assignment of variables are automatically declared to have global scope

 

 

 Second, the local scope

Variables declared inside a function belonging to a local variable, only local scope, external inaccessible.

It may cause two problems:

(1) an inner layer may cover the outer variable variable (variable lift) (2) for counting loop variable leak as a global variable (the example does not)

 

 

 

Third, the block-level scope

Let const variable declared valid in the current block region

Let const variable is defined by the function can not be output, there is no variable lift

To variable in order to use (not defined) in the statement

let const and can not be declared under the same scope variables of the same name

Variables declared const value must not be changed (except for composite type data)

 

 

 

As long as there is a block-level scope letand const commands, variables declared it would "set" (binding) this area is no longer subject to external influences (temporary dead zone)

With variables declared const and let not mount the top-level object (not output window.X form)

Guess you like

Origin www.cnblogs.com/lian-dong/p/11515050.html