Differences between local variables and global variables in javascript
Javascript has two types of variables: local variables and global variables. Of course, this article is to help you really distinguish these two variables.
First, local variables are called only within the function declared in this variable. Variables that can be called in the entire code when the global variable is used. Of course, it cannot be understood literally. I will introduce it in detail below:
As we all know, variables must be declared with the var keyword. However, variables can also be implicitly used in javascript, that is, they can be directly used without declaration. In addition, javascript always treats variables declared implicitly as global variables.
For example:
Copy codeThe Code is as follows:
Function myName (){
I = 'yuanjianhang ';
}
MyName ();
Function sayName (){
Alert (I );
}
SayName ();
Output result: yuanjianhang
This indicates that variable I is a global variable. If you change the code above to the following:
Copy codeThe Code is as follows:
Function myName (){
Var I = 'yuanjianhang ';
}
MyName ();
Function sayName (){
Alert (I );
}
SayName ();
At this time, the browser will not have any output results, because I is defined in the function myName, so it is only a local variable of myName and cannot be called externally.
Now let's look at the following code:
Copy codeThe Code is as follows:
Function myName (){
I = 'yuanjianhang ';
}
MyName ();
Function sayName (){
Alert (I );
}
SayName ();
Now let's make a change and remove myName ();. The Code is as follows:
Copy codeThe Code is as follows:
Function myName (){
I = 'yuanjianhang ';
}
Function sayName (){
Alert (I );
}
SayName ();
At this time, the browser will not respond. Although I is a global variable, the function myName () is not called, so it is equivalent to declaring I, but does not assign any value to I, so there is no output.
Similarly, if you change the preceding example:
Copy codeThe Code is as follows:
Function myName (){
I = 'yuanjianhang ';
}
Function sayName (){
Alert (I );
}
SayName ();
MyName ();
In this case, no results are output. When javascript code is executed from top to bottom, the sayName () function checks the value of variable I when it is called. At this time, the function myName has not been executed, that is to say, I has not been assigned a value, so no result is output.
For better understanding, here is another example:
Copy codeThe Code is as follows:
Var I = 'yuanjianhang ';
Function myloveName (){
I = 'guance ';
}
MyloveName ();
Function myName (){
Alert (I );
}
MyName ();
What is the result of this time?
The answer is guanxi.
First, the original value of I is yuanjianhang. However, after calling the myloveName () function, change the value of I to guanxi. Therefore, the final output result is guanxi.
If you change the code:
Copy codeThe Code is as follows:
Var I = 'yuanjianhang ';
Function myloveName (){
Var I = 'guance ';
}
MyloveName ();
Function myName (){
Alert (I );
}
MyName ();
At this time, the result is yuanjianhang. Because the two I in the Code are different, one is global, and the other is local, it can be understood that although the two I have the same name, however, they are actually different in nature. They seem to have two people with the same name. Although they have the same name, they are not the same person.
If you transform the code to the following:
Copy codeThe Code is as follows:
Var I = 'yuanjianhang ';
Function myloveName (){
I = 'guance ';
}
Function myName (){
Alert (I );
}
MyName ();
MyloveName ();
I believe you can calculate the result by yourself. The result is yuanjianhang.
Since the global variables can be called inside the function, the following is the case:
Copy codeThe Code is as follows:
Var I = 'yuanjianhang ';
Function myloveName (){
I = 'guance ';
Alert (I );
}
MyloveName ();
What is the value of the variable?
Let's analyze:
First, the global variable I is assigned the value yuanjianhang.
Next, the myloveName () function is called, and the global variable I is assigned a new value: guanxi
So the result must be: guanxi.
What if we advance alert, like this:
Copy codeThe Code is as follows:
Var I = 'yuanjianhang ';
Function myloveName (){
Alert (I );
I = 'guance ';
}
MyloveName ();
What is the result at this time?
Verified result: undefined
If the code is like this:
Copy codeThe Code is as follows:
Var I = 'yuanjianhang ';
Function myloveName (){
Alert (I );
}
MyloveName ();
The result of I is: yuanjianhang
Why does the above undefined occur, because the code execution sequence is from top to bottom, and I is not defined before I is output. So we can see from here that when using code, the variable Declaration must be put in front of the Code to avoid similar problems!
Likewise:
Copy codeThe Code is as follows:
Var I = 'yuanjianhang ';
Function myloveName (){
Alert (I );
Var I = 'guance ';
}
MyloveName ();
In this case, the output is undefined.
Well, I have only introduced so many variables that anyone can understand. No matter how code is copied, its core will not change.
The above is all the content in this article. Do you have a better understanding of the differences between local variables and global variables in javascript? I wish you a happy New Year ~ Happy Learning.