The JavaScript authoritative guide states that JavaScript variables are available before they are declared, and that this feature of JavaScript is informally called Declaration advance (hoisting), where all variables declared in the JavaScript function (but not related to assignment) are "Advance" to the top of the function. Let's look at the examples below:
Example 1:
var aa = "Test"; function MyFunc () { console.log (' AA value: ' +AA '); var aa = "TEST"; } MyFunc ();
The Call function MyFunc () Prints the result as undefined, because the function scope of the variable (but not the assignment) is "advanced" to the top of the function, so the local variable is always valid throughout the function body, the local variable AA will overwrite the global variable AA, so the printed result is Undefined, the operating procedure is equivalent to instance 2, as follows:
Example 2:
var aa = "Test"; function MyFunc () { var AA; Console.log (' AA value: ' +AA '); = "TEST"; } MyFunc ();
The results of the operation are as follows:
JavaScript variable declaration in advance