1.Var declaration and Hosting mechanism

Variables declared in function scope or global scope with the var keyword are treated as variables declared at the top of the current scope, no matter where they are declared.


Function getValue(condition){if(condition){var value = "blue" return value} else { Return null} // We can access the variable value, whose value is undefined}Copy the code


Function getValue(condition){var value if(condition){value = "blue" return value} else {return null}}Copy the code

The declaration of the value variable is promoted to the top of the function, and the initialization remains in place. This means that the variable can be accessed in the else clause, and its value is undefined because it is not initialized yet. ES6 introduces block-level scopes to enforce control over variable declaration cycles.

2. Block level declaration

Block-level declarations are used to declare variables that cannot be accessed outside the scope of the specified block. Block-level scopes exist in:

  • Function of the internal
  • Block (scope between characters {and})

Let the statement

The syntax of a let declaration is the same as that of var, but it limits the scope of a variable to the current code block. Since let does not promote variables, we usually place the let statement at the top of a closed code block so that the entire code block can access it.


Function getValue(condition){if(condition){let value = "blue" There is no return null} // variable value, there is no}Copy the code

The value variable is no longer promoted to the top of the function after being declared by let. The flow of execution leaves the if block, and the value is destroyed immediately. If condition has a value of false, the value is never declared and initialized.

No duplicate declaration

If an identifier already exists in scope, declaring it with let will result in an error.

Const statement

1) Const is a constant. Its value cannot be changed once set. 2) Therefore each const declared constant must be initialized, as shown in the following example:

Const maxItem = 30 cosnt name // syntax error, constant not initializedCopy the code

Const and let

Both of their declarations are block-level identifiers, so constants are only valid within the current code block and are immediately destroyed once executed outside the block. Constants also do not rise to the top of the scope.

Declare an object with const

Remember that a const declaration does not allow binding changes, but does allow values to be changed. Const const const const const const const const const const const const const const const const const


Person = {name: "xiaodai"} const person = {name: "laodai"}Copy the code

In the above code, the value of person bound is an object containing a property. Changing the value of Person. name does not throw any errors because you are modifying the value contained in Person, but assigning a value to Person directly, i.e. changing its binding, will throw an error.

3. Temporary dead zones

Unlike var, variables declared by lets and const are not promoted to the top of the scope. If they are accessed before the Maiden Revolution, a reference error may be triggered. Take a look at this code:


If (condition){console.log(typeof. Value) // reference error // until variable assignment is temporary dead zone let value = "blue"}Copy the code

Since console.log(typeof. Value) statements throw errors, statements that define and initialize a variable value with a let are not executed. At this point the value is still in what the JavaScript community calls a temporary dead zone. When a JavaScript engine finds a declared variable while scanning code, it either pushes it to the top of scope (VAR) or puts it in a temporary dead zone. Accessing a variable in a temporary dead zone triggers a runtime error. A variable can only be removed from the temporary dead zone after a statement has been executed.