This article mainly introduces the differences between Javascript global variables var and non-var. For more information, see global variables, variables defined in the form of a = 1 in the function scope will be a global variable. In the global scope, you can create globally visible names in the following three forms:
The Code is as follows:
Script
Var a = 1;
B = 2;
Window. c = 3;
Script
For B = 2, it is actually the same as c. When executing this value assignment statement, it will search for a variable named B along the scope chain, the top of the scope chain has not been found, so add a property B to window and assign a value.
There are two differences between var and non-var:
1. The global variables of var cannot be deleted because delete intelligently deletes the deletable attributes of an object, and the global attributes defined by var are marked as non-deletable. Note that if the delete operation fails, the system will not throw an error. The return value of the delete operation is true | false.
2. The global variables defined by var are upgraded, but those defined by var are not. You can see the execution result of the following program:
The Code is as follows:
Script
Alert ();
Var a = 1;
Script
The Code is as follows:
Script
Alert (a); // error, a undefined
A = 1;
Script