Case 1 console. log (a); Output functiona () {} functiona () {}; vara3; Case 2 functiona () {}; vara3; console. log (a); Output 3. Why are the two outputs different? Resolution: for Case 1, the execution process can be subdivided into 1. First, a space will be applied to declare function a, and then the variable a will be executed.
Console. log (a); // output function a () {} function a () {}; var a = 3;
Case 2
Function a () {}; var a = 3; console. log (a); // output 3
Why are the two outputs different?
Resolution:
Case 1
The execution process can be subdivided
1. First, I will apply for a space, start to declare function a, and then execute the Declaration variable a, and find that the declaration of a already exists, so I will not declare it.
2. Then, execute console. log (a); that is, the output function.
3. Finally, execute the value assignment operation to convert a to 3. At this time, function a is converted to the variable type and assigned a value of 3. For verification, you can change the program to the following
a();function a() {console.log(5);};var a=3;a();
The program execution result is as follows:
Supplement
1. the following code execution result is:
var x=0;f();console.log(x);var f=function(){x=1;}f();console.log(x);function f(){x=2;}f();console.log(x);
Answer: 2 1 1
The above is the priority of variable function declaration in Javascript. For more information, see PHP Chinese website (www.php1.cn )!