JavaScript function scope and advance declaration Sharing _ Basics

Source: Internet
Author: User
Some languages, such as C and Java, have block-level scopes, where each piece of code within the curly braces has its own scope, and variables are not visible outside of the code snippet in which they are declared, but JavaScript does not have a block-level scope. JavaScript uses function scopes, where variables are defined in the body of the function that declares them and in any function that is nested within the function body, and all variables declared inside the function are always visible in the function body. This means that the variable can be used before the declaration, which is called "declaration advance", where all the variables declared in the JavaScript function are advanced to the top of the function. Let's look at an example.
Copy Code code as follows:

var test1 = "GlobalVariable";
function Test () {
Console.log (test1);
var test1 = "Localvariable";
Console.log (test1);
}

The result of this function is to output "undefined" first and then output "localvariable".
Many people will mistakenly think the result is: First output "globalvariable, then output localvariable." In fact, because of the function scope, the local variable is always defined in the whole function body, that is, the local variable in the function hides the global variable with the same name, but the local variable is actually assigned only when the program executes to the VAR statement. Therefore, the above process is equivalent to the variable declaration within the function to the top of the function body, while the variable initialization left in the original position. is equivalent to the following function
Copy Code code as follows:

var test1 = "GlobalVariable";
function Test () {
var test1; To advance a variable declaration within a function to the top of a function
Console.log (test1);
Test1 = "Localvariable"; assigning values
Console.log (test1);
}

However, if the variable is not declared in the function with Var, the situation is different.
Copy Code code as follows:

var test1 = "GlobalVariable";
function Test () {
Console.log (test1);
Test1 = "Localvariable";
Console.log (test1);
}

The result of this function is to output "globalvariable" first and then output "localvariable".
Because the test1 variable in the function body is not declared with VAR, it defaults to the global variable, and of course there is no problem in advance declaration of the variable. The first line will output "globalvariable", while the third row changes the value of the TEST1 global variable, outputting the "localvariable".

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.