JavaScript Basic Concepts (ii)---variables

Source: Internet
Author: User

Variable 1, variable definition

In JavaScript, you use var to define variables, as shown in the following format:

var name = 1;

If only the variable is declared and the variable is not initialized, then the value of the variable is undefined; (why undefined see variable section III)

Of course, you can also define multiple variables at once, as follows:

var a1 = 1, a2 = true, a3 = "Zhang San";

Since the variables in JavaScript are loosely typed, which is what we call weakly typed variables, different types of initialized variables can be placed on one line;

You can also initialize a variable in JavaScript without using Var, directly using the following method:

Age = 20;

But the variables defined in this way are global variables, and in the browser that is the variable of Windows, this involves the scope of the variable, what does it mean? That is, a variable defined with VAR is a local variable that defines the scope of the variable ;

2. Global variables and local variables

What do you mean by saying that a variable defined with VAR is a local variable that defines the scope of the variable ? For example, consider the following:

You can see the variables defined inside the function test, and when they are accessed outside the function, an error is made, meaning that the variable is undefined and why does it occur? This is because the variable defined inside the function is destroyed when the function is executed, so it is inaccessible outside the function , which means that the scope of the title variable is only inside the function test, that is, the title is the local variable of the function test;

Look at the second example:

In this example, two variables are defined outside the function, namely the title defined by Var and the name directly defined, and the results of the operation can be found, both inside and outside the function, that both variables can be accessed, indicating that both variables are global variables. So what is the scope of the global variable? the scope of the global variable iswindow, which is the current browser window, which can be seen through the example above;

Let's look at a third example:

It is strange to see here that the first example is not to say that the scope of a variable defined inside a function is inside a function? Now why is it accessible outside of the function? The first example differs from this example in that the variable defined inside the function in the first example uses the var operator, but there is no var operator, what does it mean? In other words, a variable defined inside a function, if not using the var operator, is a global variable.

The above three examples are in normal mode, but ECMAScript 5 in the strict mode, about the strict mode of my previous article has been introduced, we can go to see, of course, you can ask the mother, we put the above example into a strict mode:

After the strict mode is turned on, the printed result is as shown above, that is, in strict mode, the definition of the variable must use VAR, otherwise the direct error.

Finally, note that in strict mode, you cannot use eval and arguments as variable names, or you will report a syntax error .

3. Basic variables and reference variable storage structure

This part of the content originally I was read http://www.cnblogs.com/sharpxiajun/p/4133462.html this blog only understand, originally wanted to write out, but the time will inevitably need to write some of the original pictures or ideas, later think or not write, Directly attach the connection, want to see the people themselves to see the original, write really good.

Variable part for the time being, the next article begins to write the data type;

JavaScript Basic Concepts (ii)---variables

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.