β var
π a few small examples
var a=10;
var a=20;
console.log(a)// a result of 20 indicates that the declaration can be repeated and the value changes
Copy the code
console.log(a)// The result is underfind
var a=50;
// Indicates that the variable declared by var has a declaration promotion. Use can precede declaration
Copy the code
var a = [];
for (var i = 0; i < 10; i++) {
a[i] = function () {
console.log(i);// Result is 10
}
}
a[5] ();Copy the code
Summary: We can find from the abovevar
Declared variables have their strengths and weaknesses, so Es6 has added a commandlet
Is also used to declare variables
β let
π a few small examples
/* Example: 1*/
let a = 10;
let a = 20;// result: error, Identifier 'a' has already been declared
console.log(a);
Copy the code
console.log(a);// Error: Cannot access 'a' before initialization
let a = 10;
Copy the code
The first linelet a = 10; The second lineif (true) {third lineconsole.log(a);// Error: 'a' cannot be accessed before initialization.In the fourth rowlet a = 30; // block-level scopeLine 5} line 6console.log(a);/ / 10
Copy the code
In the last small example, we found that we had already defined a, so why would printing a after it give an error? There are two concepts to mentionTemporary dead zone
andBlock-level scope
π temporary dead zones and block-level scopes
Block-level scope: With the let statement after a variable, if there are any {} can form a scope, we call this area within the {} block level scope, as we are in the first line declares the let a = 10 in the second to the fifth line has a block-level scope, so run to here, we want to be in this block level scope will be an error in the a group. Why do you need block-level scopes?
Case one: the inner variable may override the outer variable case two: the counter variable in the loop becomes a global variable.Copy the code
Temporary dead zone: As long as a variable is declared with let in a block-level scope, the variable is bound to that scope and is available only in that scope.
β Var and let comparison
let
There is no declaration promotion and an error will be reported if called before the declaration;var
Can be called before declaration with the value undefined.let
Duplicate declarations are not allowed (as shown in Example 1 above),var
You can reread the statement.let
Be careful when using itTemporary dead zones and block-level scoping issues
β const
Const is used the same way as let.
Similarities:
- There is no claim promotion
- Temporary dead zone
- Block-level scope
- Duplicate declarations are not allowed.
Difference:
- Const is used to declare constants; A variable declared by const must be assigned a value that cannot be changed
let a;
a = 10;
const b; //Missing Initializer in const declaration The variable declared by const is not initialized.
const b = 20;
b = 30;/ / an error!
console.log(b);
Copy the code
const arr = [10.20.30];
arr.push(40);
console.log(arr);
Copy the code
β summary
- A variable defined by const that stores a value of a complex data type is not modified as long as the address is unchanged.
- For general use
let
andvar
Const is often used to fetch element nodes, introduce modularity, etc.