Detailed explanation of the difference between local variables and global variables in javascript

JavaScript has two kinds of variables: local variables and global variables. Of course, our article is to help you really distinguish between these two variables.

First of all, a local variable means that it can only be called inside the function in which the variable is declared. A global variable is a variable that can be called throughout the code. Of course, the literal understanding is definitely not clear. I will introduce it in detail below: As
we all know, variables need to be declared with the var keyword. However, variables can also be used implicitly in javascript, that is, they can be used directly without declaring them. Also, do note that javascript always treats implicitly declared variables as global variables.
E.g:

 

Copy the code The code is as follows:


function myName() {
 i = 'yuanjianhang';
}
myName();
function sayName() {
 alert(i);
}
sayName();

 

The output is: yuanjianhang

This shows that the variable i is a global variable. If the above code is changed to the following:

 

Copy the code The code is as follows:


function myName() {
 var i='yuanjianhang';
}
myName();
function sayName() {
 alert(i);
}
sayName();

 

At this point, the browser will not have any output result, because i is defined in the function myName, so it is only a local variable of myName and cannot be called externally.
 
Now go back and look at the following code:

 

Copy the code The 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 the code The code is as follows:


function myName() {
 i = 'yuanjianhang';
}
function sayName() {
 alert(i);
}
sayName();

 

 At this point, the browser will not react. Because although i is a global variable, the function myName() is not called, so it is equivalent to declaring i, but not assigning any value to i, so there is no output.
Similarly, if the above example is changed to:
 

Copy the code The code is as follows:


function myName() {
 
 i = 'yuanjianhang';
}
function sayName() {
 alert(i);
}
sayName();
myName();

 

In this case, no result will be output. The execution of the javascript code is from top to bottom. When the sayName() function is called, the value of the variable i will be checked. At this time, the function myName has not been executed yet, that is to say, i has not yet been executed. is assigned, so no result is output.
 
For better understanding, here is another example:

 

Copy the code The code is as follows:


var i = 'yuanjianhang';
function myloveName() {
 i = 'guanxi';
}
myloveName();
function myName() {
 alert(i);
}
myName();

 

What is the result this time?
The answer is guanxi
First, the original value of i is yuanjianhang, but when the myloveName() function is called, the value of i is changed to guanxi, so the final output is guanxi.

If you change the code to:

 

Copy the code The code is as follows:


var i = 'yuanjianhang';
function myloveName() {
 var i = 'guanxi';
}
myloveName();
function myName() {
 alert(i);
}
myName();

 

The result at this time is Yuanjianhang, because the two i in the code are different, one is global and the other is local. It's like there are two people with the same name, although they have the same name, they are not the same person.

If you change the code to this:

 

Copy the code The code is as follows:


var i = 'yuanjianhang';
function myloveName() {
 i = 'guanxi';
}
function myName() {
 alert(i);
}
myName();
myloveName();

 

I believe that everyone can calculate the result by themselves, and the result is Yuanjianhang.

Since global variables can be called inside functions, what about the following situation:

 

Copy the code The code is as follows:


var i = 'yuanjianhang';
function myloveName () {
  i = 'guanxi';
 alert (i);
}
myloveName ();

 

What is the value of the variable at this time?

Let's analyze:

First, the global variable i is assigned as: yuanjianhang.

Next, the myloveName() function is called, and the global variable i is re-assigned a new value: guanxi

So the result must be: guanxi.

What if we advance the alert, like this:

 

Copy the code The code is as follows:


var i = 'yuanjianhang';
function myloveName () {
  alert (i);
 i = 'guanxi';
}
myloveName ();

 

What is the result then?
After verification, the result is: undefined
What if the code is like this:

 

Copy the code The code is as follows:


var i = 'yuanjianhang';
function myloveName () {
  alert (i);
}
myloveName ();

 

The result of i at this time is: yuanjianhang

Why does the above undefined situation occur, because the execution order of the code is from top to bottom, and i is not defined before output i. So it can be seen from this that when using the code, the variable declaration must be placed in front of the code to avoid similar problems!

Similarly:

 

Copy the code The code is as follows:


var i = 'yuanjianhang';
function myloveName () {
 alert (i);
 var i = 'guanxi';
 
}
myloveName ();

 

In this case, it will also output: undefined

More examples are as follows:

http://www.jb51.net/article/61442.htm

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325986716&siteId=291194637