All those defined outside the function body are global variables, and those defined inside the function body are local variables. The definition here refers to the declaration by var.
JavaScript has an implicit global concept, which means that any variable you do not declare will become a global object property. For example:
The code copy is as follows:
function test(){
myname = "huming";
alert(myname);
}
test(); // "huming"
alert(myname); //"huming"
The two results are the same, indicating that myname is a global variable.
So, is there a difference between implicit global variables and well-defined global variables? . There must be an answer, see the following example:
The code copy is as follows:
// Define three global variables
var global_test1 = 1;
global_test2 = 2; // Negative textbook
(function () {
global_test3 = 3; // Negative textbook
}());
// Attempt to delete
delete global_test1; // false
delete global_test2; // true
delete global_test3; // true
// Test the deletion
alert(typeof global_test1); // "number"
alert(typeof global_test2); // "undefined"
alert(typeof global_test3); // "undefined"
From the above example, we can see that global_test1 defined by var outside the function cannot be deleted, while global_test2 and global_test3 not defined by var are deleted (whether it was created in the function body or not).
In summary, global variables declared by var outside the function cannot be deleted, while implicit global variables can be deleted.
Note here: JavaScript has a behavior called "hoisting" (hanging/stop parsing/preparsing).
Let's use an example to illustrate:
The code copy is as follows:
var myname = "huming"; //Declare global variables
function test() {
alert(myname);
var myname = "local_huming";
alert(myname);
}
test();
Do you guess the content of alert is consistent twice? ? Obviously inconsistent, is it necessary to say that consistency? . The actual output is: "undefined", "local_huming".
The above example is equivalent to
The code copy is as follows:
var myname = "huming"; //Declare global variables
function test() {
var myname;
alert(maname);<br> myname = "local_huming";
alert(myname); // "local"
}
test();
The first alert output myname is not the global variable you think, but the local variable within a scope (a function body). Although it has not been declared, it has been regarded as a declaration. This is what is called "hoisting".
This should be understood. If you use a variable in the function body and redeclare it later, an error may occur.
Writing specifications:
The code copy is as follows:
function test() {
var a = 1,
b = 2,
c = a + b,
d = {},
e,
f;
// function body...
}
The benefits are:
1. All local variables are defined at the beginning of the function, which is convenient for searching;
2. Prevent logical errors from being used before the variable is defined.
Have you understood the variable declaration of JavaScript? The above content is very detailed and easy to understand. The final summary is also very pertinent. Don’t miss it.