function scope of JavaScript

Source: Internet
Author: User

  Students with experience in C programming should know "block scope": Each piece of code within the curly braces has its own scope and is not visible outside the code snippet that declares them. In JavaScript there is no block-level scope, and JavaScript instead uses "function scope": Variables are defined in the body of the function in which they are declared and in any function within which the body of the function is nested.

Light with text explanation plain tasteless, first to paragraph let you spirits code:

function myTest (num) {    var i = 0;     if (num = = 222)        {var j =0        ;  for (var k=0; k<2; k++) {            console.log (k);        }        Console.log (k);    }    Console.log (i);    Console.log (j);    Console.log (k);    Console.log (m);} MyTest (111);

Please read the code carefully, think carefully, and answer questions carefully. Here is the result of running the browser, test your answer!

If you answer correctly and understand all the reasons, then there is no need to look at it, if you have not yet understood the reason and have a curious, intended to explore the heart, then down to seriously read this blog.

If you do not understand, then continue to look at the explanation: JavaScript function scope refers to all variables declared within the function is always visible in the function body, which means that the variable is even available before the declaration. The JavaScript feature is informally referred to as declaration in advance, i.e. all variables declared in the JavaScript function (but not related to assignment) are advanced to the top of the function body. So, the code in the example above is equivalent to

function myTest (num) {    var  i,j,k;     = 0;     if (num = = 222)        {=0        ;  for (k=0; k<2; k++) {            console.log (k);        }        Console.log (k);    }    Console.log (i);    Console.log (j);    Console.log (k);    Console.log (m);}

So even if the code is running without entering the IF condition statement, but the variable i,j,k is already declared (even if the K declaration declared in the for loop is advanced), just I is initialized to 0, and J,k is not initialized, so the j,k output is undefined. As for M, because it has not been declared at all, so the call when the error. Now understand, and then a question to check their own situation:

var str1= "Out1"; var str2 = "Out2"; function myTest () {    console.log (str1);    Console.log (STR2);     var str2 = "inner";    Console.log (STR2);}
MyTest ();

What was the result? Here's a browser answer, check your heart's answer!

A suggestion for JS learners: Try to put the variable declaration at the top of the function body instead of putting the declaration where the variable is used, so that the code clearly reflects the scope of the real variable.

Here is the next heartfelt to you crossing five welcome (O (∩_∩) o~): Welcome reprint, Welcome collection, welcome comments, Welcome to praise, welcome recommendation!

function scope of JavaScript

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.