JavaScript Scope
Scope is the set of variables that are accessible.
JavaScript Scope
In JavaScript, objects and functions are also variables.
In JavaScript, scope is the set of accessible variables, objects, and functions.
JavaScript function scope: Scope is modified inside a function.
JavaScript Local Scope
Variables declared inside a function are local variables and have local scope.
Local variables: can only be accessed inside the function.
Example
//
The carName variable cannot be used here
function
myFunction
(
)
{
var
carName
=
"
Volvo
"
;
//
The carName variable can be used inside the function
}
Try it »
Because local variables only apply inside a function, different functions can use variables with the same name.
Local variables are created when the function begins executing and are automatically destroyed after the function finishes.
JavaScript Global Scope
A variable defined outside a function is a global variable.
Global variables have Global scope: they can be used by all scripts and functions on the web page.
Example
var
carName
=
"
Volvo
"
;
//
The carName variable can be used here
function
myFunction
(
)
{
//
The carName variable can be used inside the function
}
Try it »
If a variable is not declared inside a function (without the var keyword), it is a global variable.
In the following example, carName is inside a function but is a global variable.
Example
//
The carName variable can be used here
function
myFunction
(
)
{
carName
=
"
Volvo
"
;
//
The carName variable can be used here
}
Try it »
JavaScript Variable Lifetime
A JavaScript variable's lifetime is initialized when it is declared.
Local variables are destroyed after the function finishes executing.
Global variables are destroyed when the page is closed.
Function parameters
Function parameters only work inside the function; they are local variables.
Global variables in HTML
In HTML, the global variable is the window object, so the window object can call undeclared local variables inside a function (those without var).
**Note:** All global variables belong to the window object.
Example
//
window.carName can be used here
function
myFunction
(
)
{
carName
=
"
Volvo
"
;
}
Try it »
Did you know?
|
| Your global variables or functions can override variables or functions of the window object.
Local variables, including the window object, can override global variables and functions. | | --- | --- |
In JavaScript, local variables inside a function usually cannot be accessed directly from an outer scope, but there are several ways to expose them to the outer scope, as follows:
- **Via the global object:** Inside a function, you can make a local variable globally accessible by assigning it to a property of the window object. For example, using the statement window.a = a; you can access that local variable's value outside the function via window.a.
- **Defining a global variable:** If you declare a variable inside a function without using var、let or const keywords such as var or let, that variable is treated as a global variable and can be accessed outside the function. However, this is generally not recommended, as it may cause unexpected side effects and make the code hard to maintain.
- **Return value:** By using return a return statement inside the function, you can return the local variable's value and receive it outside the function. In this way, although the local variable itself is not exposed, its value can be passed to the outside through the function call.
- **Closures:** The closure feature in JavaScript allows an inner function to access the local variables of an outer function. Even after the outer function has finished executing, its local variables can still be referenced by the inner function.
- **Properties and methods:** Variables and functions defined in the global scope become properties and methods of the window object, so you can omit window when calling them and use the variable or function name directly.