As follows
Copy Code code as follows:
There are actually two steps:
1 Initialize A to undefined
2 A assigned value 3
So there will be some "strange" phenomenon, that is, JS variables can be used first after the declaration. This is not allowed in Java.
Copy Code code as follows:
System.out.println (a);
int a = 1;
compilation does not pass. But JS can, as follows
Copy Code code as follows:
Although it is undefined, it will not be an error. Note A does declare, and it is undefined.
If only "alert (a)", there is no "var a", the JS engine will complain.
Copy Code code as follows:
FF in the following
Although you can use the declaration first, this will result in the loss of the assignment. As follows
Copy Code code as follows:
The output is still undefined rather than 1.
Another example,
Copy Code code as follows:
Alert (' A ' in window); True
var A;
Although the code form is written on alert, the engine will still automatically process the declaration of VAR first. The last output is true.
It's not hard to understand this. The following code runs the result
Copy Code code as follows:
if (! () A "in Window") {
var a = 1;
}
alert (a);