Basic concepts of JavaScript (2) --- variables and basic concepts of javascript

Source: Internet
Author: User

Basic concepts of JavaScript (2) --- variables and basic concepts of javascript
Variable 1. variable definition

Use var in Javascript to define variables. The specific format is as follows:

Var name = 1;

If only the variables are declared and the variables are not initialized, the value of the variable is undefined. (For details about undefined, see section 3 of the variable)

Of course, you can also define multiple variables at a time, as shown below:

Var a1 = 1, a2 = true, a3 = "James ";

Because the variables in javascript are loose, that is, the weak variables we call, initialization variables of different types can be placed in one row;

When defining variables in javascript, you can use the following method to initialize variables without using var:

Age = 20;

However, variables defined in this way belong to global variables, which are also windows variables in the browser. The scope of variables is involved here. What does it mean? That is to say, the variable defined by var is the local variable in the scope of the variable;

2. Global and local variables

The variables defined using var are local variables in the scope of the variable. What does it mean? An example is as follows:

 

We can see that the variable defined in function test reports an error during external function access, which means the variable is not defined. Why is this error? This is because the variable defined in the function is destroyed after the function is executed, and thus cannot be accessed outside the function, it indicates that the scope of the title variable is only within the test function, that is, the title is the local variable of the test function;

Let's 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. The running result shows that either inside the function or outside the function, both variables can be accessed, indicating that the two variables defined are global variables. What is the scope of global variables? The scope of global variables is window, that is, the current browser window, which can be seen through the above example;

Let's look at the Third example:

 

It may be strange to see that the first example does not mean that the scope of the variables defined within the function is within the function? Why can I access it again outside the function? The first example differs from this example. In the first example, the variable defined in the function uses the var operator, but there is no var operator. What does it mean? That is to say, if the var operator is not used for variables defined within the function, the variable is a global variable.

The above three examples are in the normal mode, but the strict mode is added to ECMAScript 5. I have already introduced the strict mode in the previous article. You can take a look at it, of course, you can also ask du Niang by yourself. Let's change the above example to a strict mode:

 

After the strict mode is enabled, the output is shown as above. That is to say, in the strict mode, the variable must be defined using var; otherwise, an error is reported directly.

Note that eval and arguments cannot be used as variable names in strict mode. Otherwise, a syntax error is reported.

 

3. Storage Structure of basic variables and reference Variables

I have read this part of the content.

 

This is the case for the variable section. The next article begins to write the data type;

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.