Set of accessible variables in the scope.
JavaScript Scope
Set of accessible variables in the scope.
JavaScript Scope
In JavaScript, objects and functions are also variables.
In JavaScript, the scope is a set of accessible variables, objects, and functions.
JavaScript function scope: the scope is modified within the function.
JavaScript local scope
Variables are declared in the function, and the variables are local scopes.
Local variables: they can only be accessed within the function.
Instance
// The carName variable cannot be called here
Function myFunction (){
Var carName = "Volvo ";
// The carName variable can be called in the function.
}
Try again»
Because local variables only apply to functions, different functions can use variables with the same name.
The local variable is created when the function starts to be executed. After the function is executed, the local variable is automatically destroyed.
JavaScript global variables
A variable is defined outside a function, that is, a global variable.
Global variables have a global scope: All scripts and functions on the web page can be used.
Instance
Var carName = "Volvo"; // The carName Variable function myFunction () can be called here {// The carName variable can be called in the function}
If the variable is not declared in the function (the var keyword is not used), the variable is a global variable.
In the following example, carName is in the function but is a global variable.
Instance
// The carName Variable function myFunction () {carName = "Volvo" can be called here; // The carName variable can be called here}
JavaScript variable Lifecycle
The JavaScript variable lifecycle is initialized when it is declared.
Local variables are destroyed after the function is executed.
Global variables are destroyed after the page is closed.
Function Parameters
Function parameters only work in the function. They are local variables.
Global variables in HTML
In HTML, global variables are window objects: All data variables belong to window objects.
Instance
// Here you can use window. carNamefunction myFunction () {carName = "Volvo ";}
Do you know?
Your global variables or functions can overwrite the variables or functions of the window object.
Local variables, including window objects that can overwrite global variables and functions.
The above is the content in the JavaScript scope of the [JavaScript tutorial]. For more information, see the PHP Chinese website (www.php1.cn )!