SoFunction
Updated on 2025-04-09

A brief analysis of JavaScript declaration variables

JavaScript variable declaration statements are executed first before other code wherever they appear. Using the var keyword to declare a variable's scope is the current execution context, which may be a peripheral function, or, when the variable is declared outside the function body, it is a global variable.

All those defined outside the function body are global variables, and those defined inside the function body are local variables. The definition here refers to the declaration by var.

JavaScript has an implicit global concept, which means that any variable you do not declare will become a global object property. For example:

function test(){
myname = "huming";
alert(myname);
}
test();// "huming"
alert(myname);//"huming" 

The two results are the same, indicating that myname is a global variable.

So, is there a difference between implicit global variables and well-defined global variables? . There must be an answer, see the following example:

// Define three global variablesvar global_test = ;
global_test = ; // The negative textbook(function () {
global_test = ; // The negative textbook}());
// Attempt to deletedelete global_test; // false
delete global_test; // true
delete global_test; // true
// Test the deletionalert(typeof global_test); // "number"
alert(typeof global_test); // "undefined"
alert(typeof global_test); // "undefined" 

From the above example, we can see that global_test1 defined by var outside the function cannot be deleted, while global_test2 and global_test3 not defined by var are deleted (whether it was created in the function body or not).

In summary, global variables declared by var outside the function cannot be deleted, while implicit global variables can be deleted.

Note here: JavaScript has a behavior called "hoisting" (hanging/stop parsing/preparsing).

Let's use an example to illustrate:

var myname = "huming"; //Declare global variablesfunction test() {
alert(myname);
var myname = "local_huming";
alert(myname);
}
test();

Do you guess the content of alert is consistent twice? ? Obviously inconsistent, is it necessary to say that consistency? . The actual output is: "undefined", "local_huming".

The above example is equivalent to

var myname = "huming"; //Declare global variablesfunction test() {
var myname;
alert(maname);<br>myname = "local_huming";
alert(myname); // "local"
}
test(); 

The first alert output myname is not the global variable you think, but the local variable within a scope (a function body). Although it has not been declared, it has been regarded as a declaration. This is what is called "hoisting".

This should be understood. If you use a variable in the function body and redeclare it later, an error may occur.

Writing specifications:

function test() {
var a = ,
b = ,
c = a + b,
d = {},
e,
f;
// function body...
}

The benefits are:

1. All local variables are defined at the beginning of the function, which is convenient for searching;

2. Prevent logical errors from being used before the variable is defined.

In javascript, a variable name (name) has four ways to enter scope (scope)

The language is built-in, and all scopes have this and arguments keywords

Formal parameters, the parameters of the function are valid throughout the scope

Function declaration

Variable declaration

The four orders listed above are also from the order of priority from the highest to the bottom. Once a variable name has been declared, it cannot be overwritten by other lower-priority variable declaration forms.