There are two new commands in ES6: Let and Const.
Let command:
let is used to declare a variable, similar to Var, but the declared variable is valid only in the code block, there is no promotion of the variable, there is a temporary dead zone.
1. Valid only in code blocks
Unlike the Var command, a let declared variable is only valid in a block of code, such as
{ = 1;
var b = 2;}
Console.log (b); Print 2;console.log (a); This would cause error.
2. There is no variable promotion
The so-called variable promotion, which is the same scope, is called before declaring the variable.
function Test () {
temp = "Test" console.log (temp); // Would pint test. var temp;}
However, a variable that is declared with let does not have a variable promotion, meaning that it must be used after declaring the variable:
function Test () { = "Test" console.log (temp); // reference error. Let temp;}
3. Temporary Dead Zone
The so-called temporary Dead Zone refers to the effect of declared variables that do not accept variables with the same name. For example:
var tmp = 123; if (true) { // referenceerrorlet tmp;}
The existence of a temporary dead zone makes the TypeOf no longer a safe operation (because it is possible that a variable cannot be promoted because it causes a reference error)
4. In addition, let does not allow duplicate declarations at the same scope
At the same block-level scope, Var declares that the same variable is possible, but let is not allowed.
5. To get a block-scoped variable, you can use the Do command:
Do { = f (); * t + 1;};
Const command
The const command is used to declare read-only constants, like let, where scopes are block-level scopes, there are no variable elevation, and there are temporary dead zones.
When a const command declares a constant, only the address that the variable name points to is guaranteed to be the same, that is, if a const-declared constant points to an object, it does not guarantee that the property values in the object will not be changed. To ensure that the object property values are not changed, use Object.freeze () to freeze the object.
JavaScript Learning Notes-ES6 Learning (ii) Let and const