In JavaScript, the scope of a variable is divided by function-the variable is valid within the scope of a function. Like what:
Copy Code code as follows:
var f = false;
if (true) {var f = true;}//This time F is within the IF, that is, within the block, equivalent to or globally scoped
Alert (f)//So the result is true
Again, the following example:
Copy Code code as follows:
var f = false;
function Test () {
var F = true; This is a variable defined within a function that is valid inside a function and is released when the function has finished executing
}
Test ();
Alert (f)//result is false and does not change because of the execution of Test ()
Again, a global variable declared in JavaScript can be considered a property of a Window object, such as:
Copy Code code as follows:
var test = "This is a test";
Alert (window.test = = test)//result is true
This just validates that the global variable is also the property of the Window object.
Finally, let's look at
[Code]
function Test () {
F = false;
}
Test ();
Alert (f)//result is False
[HTML]
Then, if you do not add var (implicit declaration) when declaring a variable, it is also considered a global variable, although it is defined within the function.