JavaScript variable declaration _ javascript skills

Source: Internet
Author: User
This article introduces the javascript variable declaration in detail and makes a detailed analysis through examples. It is a very good article. We recommend this article to the newly started jser. Global variables are defined in the function body and local variables are defined in the function body. The definition here is declared through var.

JavaScript has an implicit global concept, which means that any variable you do not declare will become a global object attribute. For example:

The Code is as follows:


Function test (){
Myname = "huming ";
Alert (myname );
}
Test (); // "huming"
Alert (myname); // "huming"

The two results are the same, indicating that myname is a global variable.

There is no difference between an implicit global variable and a clearly defined global variable .. The answer is yes. Let's look at the example below:

The Code is as follows:


// Define three global variables
Var global_test1 = 1;
Global_test2 = 2; // textbook
(Function (){
Global_test3 = 3; // textbook
}());
// Try to delete
Delete global_test1; // false
Delete global_test2; // true
Delete global_test3; // true
// Test the deletion.
Alert (typeof global_test1); // "number"
Alert (typeof global_test2); // "undefined"
Alert (typeof global_test3); // "undefined"

As shown in the preceding example, global_test1 defined by var cannot be deleted, global_test2 and global_test3, which have not been defined by var, are deleted (whether they are created in the function body or not ).

In summary, the global variables declared through var in the function in vitro cannot be deleted, while the implicit global variables can be deleted.

Note: JavaScript has a behavior called "hoisting" (mounting, top resolution, and pre-resolution ).

Here is an example:

The Code is as follows:


Var myname = "huming"; // declare a global variable
Function test (){
Alert (myname );
Var myname = "local_huming ";
Alert (myname );
}
Test ();

Do you guess the contents of alert are the same twice ?? Obviously, they are inconsistent. Do you still need to say they are consistent .. The actual output is: "undefined", "local_huming ".

The above example is equivalent

The Code is as follows:


Var myname = "huming"; // declare a global variable
Function test (){
Var myname;
Alert (maname );
Myname = "local_huming ";
Alert (myname); // "local"
}
Test ();

The myname output by alert for the first time is not a global variable, but a local variable in the same scope (a function body) as it. Although it has not been declared, it is considered as a declaration. This is the so-called "hoisting ".

This should be clear. If you use a variable in the function body and then declare it again, an error may occur.

Writing specifications:

The Code is as follows:


Function test (){
Var a = 1,
B = 2,
C = a + B,
D = {},
E,
F;
// Function body...
}


Benefits:

1. All local variables are defined at the beginning of the function to facilitate searching;

2. prevent logical errors used before variables are defined.

Do you know about the javascript variable declaration? The above content is very detailed and easy to understand, and the final summary is also very pertinent. Don't miss it.

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.