Differences between local variables and global variables in javascript _ javascript skills

Source: Internet
Author: User
This article mainly compares and analyzes the differences between local variables and global variables in javascript. It is a very good article. It is worth reading carefully and recommending it to our friends. 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:

The 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:

The 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:

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:

The 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:

The 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:

The 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:

The 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:

The 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:

The 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:

The 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:

The 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:

The 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.