About global variables and local variables

Source: Internet
Author: User

The variable is not familiar with js learning and programming languages. I will not elaborate on official definitions here. There are too many variables on the Internet. Today we will understand it from our daily lives.

1. What is a variable?
For example:
A water cup contains water, which is a variable;
A beer bottle is a variable;
A variable is a carrier and a medium.

2. Define Variables
Var a = 12; // typeof a = Numer
Var a = 'aaa' // typeof a = string
It can be seen that the type of the variable depends on the value assigned to him.
For example, a cup is filled with water, a wine is a glass, and a vinegar is a vinegar bottle.

3. Variable type
Variable types include: basic type and reference type
Basic types include Number, String, Boolean, Undefined, and Null.
Reference Type: Mostly Obiect (object)
The basic type value is a simple data stored in the stack memory, which occupies a position in the memory;
The reference type value is the object stored in the heap memory, and the address is stored in the stack memory. This address points to the object in the heap memory.

1. Local Variables
Copy codeThe Code is as follows:
<Scripttype = "text/javascript">
Functionaaa ()
{
Vara = 10;
}
Functionbbb ()
{
Alert ()
}
Aaa ()
Bbb ()
</Script>

Running result: Error: "a" is not defined. a is a local variable. It only belongs to aaa and does not belong to bbb.
2. Global variable 1
Copy codeThe Code is as follows:
<Scripttype = "text/javascript">
Vara
Functionaaa ()
{
Vara = 10;
}
Functionbbb ()
{
Alert ()
}
Aaa ()
Bbb ()
</Script>

Running result: The pop-up undefined is also one of the variable types, but it is the undefined type. It is not the same as the first type of undefined.
The type of the variable is determined by the value assigned to the variable. At this time, a in the bbb function is a global variable. Although var, it is not specified, so it is undefined.
3. global variable 2
Copy codeThe Code is as follows:
<Scripttype = "text/javascript">
Vara
Functionaaa ()
{
A = 10;
}
Functionbbb ()
{
Alert ()
}
Aaa ()
Bbb ()
</Script>

Running result: 10, a is a global variable and assigned a value through the aaa function -- 10
Ps: We often call undefined, both through 1 and 2. Can it be said that undefined is undefined?

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.