There are situations in JavaScript where a variable can be accessed before declaration without throwing an exception, and a function can be called before declaration without throwing an exception.
This involves variable or function promotion!
scope
Global, function, and block scopes.
If the variable is set in global scope, it is accessible at all audit locations;
If a variable is defined within the scope of a function, then the variable can only be accessed within that function;
Similarly, variables defined in a code block can only be accessed in that code block;
Block-level action requires variables to be defined using a specific let or const keyword.
Variable ascension
Variable declarations are promoted to the top of the function, but assignments are not. Variable promotion can only occur in variables declared by var.
var a = "90";
(function(){
console.log(a)
})()
var b = "78";
(function(){
console.log(b);
var b = 9000}) ()Copy the code
Function increase
In the same way, functions can be promoted when they are defined in a declarative manner. The following code snippet:
show();
var show;
function show(){
console.log("90")
}
show = function(){
console.log(76)}Copy the code
When a function is promoted, the whole function body is promoted together
Some knowledge, too soon to forget, while the memory is still good, make a note