JavaScript variable Scope

Source: Internet
Author: User
Tags variable scope

1<! DOCTYPE html>234<meta charset= "Utf-8" >5<title></title>67<body>8<script type= "Text/javascript" >9         /*Ten a variable scope (scope) is defined in the program source code . One the area.  A Global variables have global scope, but variables declared within a function - defined in the function body only, they are local variables, the scope is local - function arguments are also local variables, which are defined only within the function body*/ the  -      //local variables have precedence over global variables with the same name -      //----> If a local variable or function argument is declared within a function with the same name as a variable and a global variable -      //----> Global variables are obscured by local variables +      varScope= "Global";//declaring a global variable -      functionCheckscope () { +            A           varScope= "Local";//declaring the same local variable at           returnScope//returns the value of a local variable, not the value of a global variable -  -  -      } - Checkscope () -  inConsole.log (Checkscope ())//"Local" -  to       //--->1--global scope You can write code without writing the VAR statement, you must use the VAR statement when declaring a local variable +Scope= "Global";//declare a global variable, even without Var -       functioncheckscope2 () { theScope= "Local";//the global variable was modified *Myscope= "Local"//here, a new global variable is declared. $           return[Scope,myscope];//returns a value of twoPanax Notoginseng       } -Checkscope2 ()//function Call theConsole.log (Checkscope2 ())//=>["local", "local"] +       //---->2--function definition can be nested, because each function has its own scope, so there will be a number of local action nesting cases A  the       varScope= "Global scope";//Global Variables +       functionCheckscope () { -           varScope= "local scope";//Local Variables $           functionnested () { $               varScope = "nested scope";//local variables within a nested scope -               returnScope//returns the value in the current scope -  the           } -Nested ()//nested function callsWuyi  the       } -Checkscope ()//= = "Nested scopes" Wu     -    /* About function scope and declaration in advance $ 1---> C-like programming-each piece of code within the curly braces has its own scope - and variables are not visible outside of the code snippet that declares them, called Block-level scopes (block scope) - There is no block-level scope in JavaScript - JavaScript instead uses the function scope: A Variables are defined in the body of the function in which they are declared and in the body of any function nested within the function body. +  the    */ -    //----1--> $    functionTest (o) { the        vari=0;//I have a definition in the whole function body the        if(typeofo== "Object"){ the            varj=0;//j is defined in the function body, not just within this code snippet the             for(vark=0;k<10;k++) {//k is defined in the function body, not just within the loop -Console.log (k);//Output Digital 0~9 in  the  the            } About  theConsole.log (k);//k already defined, output the        } theConsole.log (j);//J has been defined, but may not be initialized +    } -    //The function scope of JavaScript is that all variables declared within a function are always visible inside the function body the    //means that the variable is even available before the Declaration, and that the JavaScript feature is informally called a declaration in advanceBayi  the    //The JavaScript function declares that all variables are advanced to the inside of the function body. the    varScope= "Global"; -    functionf () { -  theConsole.log (scope);//output Undefind, "instead of" global " the        varScope= "Local";//The variable assigns the initial value here, but the variable itself is defined anywhere within the function body theConsole.log (scope);//output "local" the    } -  the  the</script> the 94</body> the

JavaScript variable Scope

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.