This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021
ES6 added the let and const commands to declare variables
let
Its use is similar to var, but the declared variable is only valid within the code block in which the let command resides.
{
let x = 10;
var y = 1;
}
x // ReferenceError: x is not defined.
y / / 1
Copy the code
The above code declares two variables, let and var, respectively, in the code block. These two variables are then called outside of the code block, with the let variable reporting an error and the var variable returning the correct value. This indicates that the variable declared by the LET is valid only in the code block in which it is located.
There is no variable promotion
The “variable promotion” phenomenon occurs when the var command is used, that is, the variable can be used before the declaration and its value is undefined. This is somewhat strange, as normal logic dictates that variables should be used only after the statement has been declared.
To correct this, the let command changes the syntactic behavior so that the variables it declares must be used after the declaration or an error will be reported.
Duplicate declarations are not allowed
Let does not allow the same variable to be declared twice in the same scope.
Therefore, arguments cannot be redeclared inside functions.
Const command
Const declares a read-only constant. Once declared, the value of a constant cannot be changed.
const a = 1.5;
a / / 1.5;
a = 1;
// TypeError: Assignment to constant variable.
Copy the code
The above code shows that changing the value of a constant causes an error.
A variable declared by const may not change its value, which means that a const, once declared, must be initialized immediately and cannot be left for later assignment.
What const actually guarantees is not that the value of the variable cannot be changed, but that the data stored at the memory address to which the variable points cannot be changed. For data of simple types (values, strings, booleans), the value is stored at the memory address to which the variable points, and is therefore equivalent to a constant. But for complex type data (mainly objects and arrays), variable pointing to the memory address, save only a pointer to the actual data, const can guarantee the pointer is fixed (i.e., always points to the other a fixed address), as far as it is pointing to the data structure of variable, is completely unable to control. Therefore, declaring an object as a constant must be done with great care.
Note:
ES6 explicitly states that if there are let and const commands in a block, the variables declared by the block to those commands form a closed scope from the start. Any time these variables are used before declaration, an error is reported.
In short, the variable is not available within the code block until it is declared using the let command. This is grammatically known as a “temporal dead zone” (TDZ).