Var, let, const var, let, const var, let, const What is ascension? What is a temporary dead zone?
1, the var
In this case, take a look at the concept of ascending
console.log(age); // undefined
var age = 18;
Copy the code
As you can see from the above code, an undeclared variable can be used even though the variable has not been declared. This situation is called promotion, and the promotion is declaration.
Look at the code like this, and it becomes clear
var age
console.log(age); // undefined
age = 18;
Copy the code
Here’s another example:
var age = 18; var age; console.log(age); / / - >?Copy the code
If you still think you’re printing undefined then you’re making a mistake, but you’re actually printing 18, so you can look at it this way
var age;
var age;
age = 18;
console.log(age);
Copy the code
Now it can be concluded that the variable declared by var will be promoted, commonly known as the promotion of variable declaration. Not only the variable will be promoted, but also the function will be promoted.
console.log(fun); ƒ fun() {} function fun() {}; var fun = 1;Copy the code
ƒ Fun () {} will be printed for the above code, even if the variable is declared after the function, which indicates that the function will be promoted and that the promotion of the variable is preferred.
As a result, you also know that variables declared using VAR are promoted to the top of the scope.
2. Let and const
Let’s take another example of let and const:
var a = 1;
let b = 1;
const c = 1;
console.log(window.b) // undefined
console.log(window.c) // undefined
function test() {
console.log(a);
let a;
};
test();
Copy the code
We first declare variables in global scope using let and const. Variables are not mounted to the window (var declarations do). This is one of the differences with var declarations.
Then we get an error if we use a before declaring a
In this case, the first reason for the error is that there is a temporary dead zone, so variables cannot be used before declaration. This is one of the advantages of let and const over var. Although variable compilation does not tell you that access is available in this scope, access comes first.
Consider: why is there such a thing as ascension?
Root cause promotion is designed to deal with situations where functions call each other
function fun1() {
fun2();
};
function fun2() {
fun1();
};
fun1();
Copy the code
If there is no promotion, then the appeal code cannot be implemented, because without promotion there is no fun1 before fun2, and then fun2 before fun1.
3, summarize
-
Var has an enhancement that allows variables to be used before declaration. Let and const cannot be used before declaration because they have temporary dead zones
-
Var declaring variables in the global scope causes variables to mount on the window, while let and const do not
-
Promotion of a function takes precedence over promotion of a variable, which moves the entire function to the top of scope. Promotion of a variable only moves the declaration to the top of scope
-
Let and const work in much the same way, except that variables declared by cosnt cannot be reassigned
—END—