Variable Scope
"The scope of a variable represents the context in which the variable exists. It specifies which variables you can access and whether you have permission to access a variable."
Variable scope is divided into local scope and global scope.
Local variables (scope at function level)
Unlike other object-oriented programming languages (such as C, Java, etc.), JavaScript does not have block-level scope (surrounded by curly braces); when , JavaScript has function-level scope, that is to say, variables defined within a function can only be accessed within the function or by functions within the function (except for closures, which we will write a topic on in a few days).
An example of function-level scope:
has no block scope:
// Don’t forget to use the var keyword
// If you declare a variable without using the var keyword, then the variable will be a global variable!
// If you don't declare your local variables with the var keyword, they are part of the global scope
var name = "Michael Jackson";
function showCelebrityName () {
console.log (name);
}
function showOrdinaryPersonName () {
name = "Johnny Evers";
console.log (name);
}
showCelebrityName (); // Michael Jackson
// name is not a local variable, it simply changes the global name variable
showOrdinaryPersonName (); // Johnny Evers
// The global variable is now Johnny Evers, not the celebrity name anymore
showCelebrityName (); // Johnny Evers
// The solution is to declare your local variable with the var keyword
function showOrdinaryPersonName () {
var name = "Johnny Evers"; // Now name is always a local variable and it will not overwrite the global variable
console.log (name);
}
// local variable Priority is greater than global variables
//If a variable in the global scope is declared again in the local scope, then when calling this variable in the local scope, the variable declared in the local scope will be called first:
var name = "Paul";
function users () {
// Here, the name variable is local and it takes precedence over the same name variable in the global scope
var name = "Jack";
// The search for name starts right here inside the function before it attempts to look outside the function in the global scope
console.log (name);
}
users (); // Jack
Global variables
All variables declared outside the function are in the global scope. In a browser environment, this global scope is our Window object (or the entire HTML document).
Every variable declared or defined outside a function is a global object, so this variable can be used anywhere, for example:
The function in setTimeout is in the global scope, so when the this keyword is used in the function, the this keyword points to the global object (Window):
In order to avoid contamination of the global scope, generally we declare as few global variables as possible.
Variable Hoisting
All variable declarations will be hoisted to the beginning of the function (if the variable is inside the function) or the beginning of the global scope (if the variable is a global variable). Let’s look at an example:
Function declaration will overwrite variable declaration
If there is a function declaration and a variable declaration (note: it is just a declaration, not assigned a value), and the variable name and function name are the same, then, They will all be prompted to the beginning of the outer scope, but the function has higher priority, so the value of the variable will be overwritten by the function.
However, if this variable or function is assigned a value, then the other one will not be able to overwrite it:
Last point, in strict mode, if you assign a value to a variable without declaring it first, an error will be reported!