Things about global variables and local variables _ basics

Source: Internet
Author: User
Variables for Learning JS, learning programming language students are familiar with, here is not to explain the official definition, online too much, today we will understand from life to his

1. What is a variable?
Like what:
A water cup contains water, the water cup is a variable;
A bottle of beer, this beer bottle is a variable;
A variable is a carrier, a medium

2. Define Variables
var a=12;//typeof a=numer
var a= ' aaa '//typeof a =string
So the type of variable depends on what value he pays.
For example, a cup, filled with water is a glass, filled with wine is glass, vinegar is vinegar bottle

3. Variable type
Variable types are divided into: base type and reference type
The basic types are: number (numeric type), string (String type), Boolean (Boolean type), Undefined (undefined), Null
Reference type: Most of them are obiect (objects)
The base type value is a simple data stored in the stack memory, which occupies a position in memory;
A reference type value is an object stored in heap memory, where the address is stored in the stack memory, which points to an object in the heap memory

1. Local Variables
Copy Code code as follows:

<scripttype= "Text/javascript" >
FUNCTIONAAA ()
{
vara=10;
}
FUNCTIONBBB ()
{
Alert (a)
}
AAA ()
BBB ()
</script>

Run Result: Error: "A" not defined, a is a local variable, he only belongs to the function AAA, does not belong to the function BBB
2. Global Variable 1
Copy Code code as follows:

<scripttype= "Text/javascript" >
Vara
FUNCTIONAAA ()
{
vara=10;
}
FUNCTIONBBB ()
{
Alert (a)
}
AAA ()
BBB ()
</script>

Run Results: Pop-up undefined, which is also one of the types of variables, but is undefined type, he is not the same as the first kind of undefined
The type of the variable is determined by the value assigned to the variable, this time A is a global variable in the BBB function, although VAR has not specified a value, so it is undefined
3. Global Variable 2
Copy Code code as follows:

<scripttype= "Text/javascript" >
Vara
FUNCTIONAAA ()
{
a=10;
}
FUNCTIONBBB ()
{
Alert (a)
}
AAA ()
BBB ()
</script>

Run Results: 10,a is a global variable and has been assigned a value via function AAA--10
PS: We often call undefined undefined, all through 1 and 2, is it possible to say that undefined≠ is undefined?

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.