1. The scope of Javascript variables is divided according to method blocks (that is, divided by a pair of curly braces {} in function). Remember, it is a function block, and the for, while, and if blocks are not the standard for dividing scopes. You can look at the following examples:
2. Before Javascript is executed, it will perform a complete analysis of the declaration part of the entire script file (including local variables) to determine the scope of the real variables. How to understand it? Look at the following example:
Third, when the global variable has the same name as the local variable, the scope of the local variable will overwrite the scope of the global variable. When leaving the scope of the local variable After scope, it returns to the scope of the global variable. When the global variable encounters the local variable, how to use the global variable? Use window.globalVariableName.
Scope of variables
1. Depending on the scope of the variable, there are two types of variables: global variables and local variables. Variables defined in a function are local variables, and local variables are only valid within the function. If the local variable and the global variable use the same variable name, the local variable will overwrite the global variable
2. The JavaScript language does not have block scope
The above is the detailed content of Detailed explanation of Javascript variable types and scope examples. For more information, please follow other related articles on the PHP Chinese website!