The difference between local variables and global variables in JavaScript _javascript tips

Source: Internet
Author: User

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

First, local variables are called only within functions declared by this variable. A variable that can be called in the entire code when it is a global variable. Of course, the literal understanding is certainly not clear, I will introduce the following in detail:
As we all know, variables need to be declared with the VAR keyword. But JavaScript can also implicitly use variables, that is, not declared, directly use. Also, be aware that JavaScript always uses implicitly declared variables as global variables.
For example:

Copy Code code as follows:

function MyName () {
i = ' Yuanjianhang ';
}
MyName ();
function Sayname () {
alert (i);
}
Sayname ();

The output is: Yuanjianhang

This means that the variable i is a global variable, if you change the above code to the following:

Copy Code code as follows:

function MyName () {
var i= ' Yuanjianhang ';
}
MyName ();
function Sayname () {
alert (i);
}
Sayname ();

At this point, the viewer will not have any output, because I is defined in the function myname, so it is only a myname local variable and cannot be called externally.

Now look back at the following code:

Copy Code code as follows:

function MyName () {
i = ' Yuanjianhang ';
}
MyName ();
function Sayname () {
alert (i);
}
Sayname ();

Now, let's Change the MyName () and remove the code as follows:

Copy Code code as follows:

function MyName () {
i = ' Yuanjianhang ';
}
function Sayname () {
alert (i);
}
Sayname ();

At this point, the browser will not have how to respond. Because although I is a global variable, the function myname () is not invoked, so it is equivalent to declaring I but not giving me any value, so there is no output.
Similarly, if the above example is changed to:

Copy Code code as follows:

function MyName () {

i = ' Yuanjianhang ';
}
function Sayname () {
alert (i);
}
Sayname ();
MyName ();

In this case, there is no output, the JavaScript code is executed from top to bottom, the value of the variable i is checked when the Sayname () function is called, and the function myname has not been executed, meaning I has not been assigned, so no results will be output.

To facilitate a better understanding, here's another example:

Copy Code code 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 Code code as follows:

var i = ' Yuanjianhang ';
function Mylovename () {
var i = ' guanxi ';
}
Mylovename ();
function MyName () {
alert (i);
}
MyName ();

The result of this is Yuanjianhang, because the two I in the code are different, one is global, the other is local, it can also be understood that, although the names of the two I are the same, but the nature of the two I is different, as if there are two people with the same name, although the same name, but not the same person.

If you transform your code into this:

Copy Code code as follows:

var i = ' Yuanjianhang ';
function Mylovename () {
i = ' guanxi ';
}
function MyName () {
alert (i);
}
MyName ();
Mylovename ();

I believe you can figure out the results, the result is Yuanjianhang.

Now that you can call global variables inside a function, here's how:

Copy Code code 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 the following:

First, the global variable i is assigned: Yuanjianhang.

Next the Mylovename () function is invoked, and the global variable I is given a new value: Guanxi

So the result must be: guanxi.

What if we put alert in advance, like this:

Copy Code code as follows:

var i = ' Yuanjianhang ';
function Mylovename () {
alert (i);
i = ' guanxi ';
}
Mylovename ();

What is the result then?
The result of the verification is: undefined
What if the code is like this:

Copy Code code as follows:

var i = ' Yuanjianhang ';
function Mylovename () {
alert (i);
}
Mylovename ();

At this point the result of I is: Yuanjianhang

Why the above undefined situation occurs because the execution order of the code is from top to bottom, and I was not defined before the output i. So from here you can see that when you use code, the declaration of the variable must be placed in front of the code to avoid similar problems!

Similarly:

Copy Code code as follows:

var i = ' Yuanjianhang ';
function Mylovename () {
alert (i);
var i = ' guanxi ';

}
Mylovename ();

In this case it will also be output: undefined

Well, I only have so many introductions about variables, and I believe that anyone can see that. Regardless of how the code replicates, its core is unchanged.

The above is the entire content of this article, small partners for JavaScript local variables and global variables are more profound understanding of the difference, I wish you a Happy New Year ~ study happy.

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.