JavaScript Scope
- Previous Page JS Exceptions
- Next Page JS Hoisting
Scope refers to the set of variables you have access to.
JavaScript function scope
In JavaScript, there are two types of scopes:
- Local scope
- Global scope
JavaScript has function scope: each function creates a new scope.
Scope determines the accessibility (visibility) of these variables.
Variables defined within a function are not accessible from outside the function (not visible).
local JavaScript variables
Variables declared within a JavaScript function become the function'slocal variables.
The scope of local variables isLocal:They can only be accessed within the function.
Example
// The code here cannot use the carName variable function myFunction() { var carName = "porsche"; // The code here can use the carName variable }
Since local variables can only be recognized within the function, the same variable name can be used in different functions.
Local variables are created at the beginning of a function and are deleted when the function is completed.
global JavaScript variables
Variables declared outside of functions will becomeGlobal variables.
The scope of global variables isGlobal:All scripts and functions on the web can access it.
Example
var carName = "porsche"; // The code here can use the carName variable function myFunction() { // The code here can also use the carName variable }
JavaScript Variables
In JavaScript, objects and functions are also variables.
Scope determines the accessibility of variables, objects, and functions from different parts of the code.
automatically global
If you assign a value to a variable that has not been declared yet, this variable will automatically becomeGlobalvariable.
This code will declare a global variable carName, even if it is assigned a value within the function.
Example
myFunction(); // The code here can use the carName variable function myFunction() { carName = "porsche"; }
Strict Mode
All modern browsers support running JavaScript in 'strict mode'.
You will learn more about how to use 'strict mode' later in this tutorial.
Global variables will not be automatically created in 'strict mode'.
Global Variables in HTML
Through JavaScript, the global scope forms a complete JavaScript environment.
In HTML, the global scope is window. All global variables belong to the window object.
Example
var carName = "porsche"; // The code here can use window.carName
Warning
Do not create global variables unless intentionally.
Your global variables (or functions) can override the window variables (or functions).
Any function, including the window object, can override your global variables and functions.
Validity of JavaScript Variables
The validity period of a JavaScript variable starts when it is created.
Local variables will be deleted when the function is completed.
Global variables will be deleted when you close the page.
Function Parameters
Function parameters are also local variables within the function.
- Previous Page JS Exceptions
- Next Page JS Hoisting