JavaScript about local variables and global variables

Source: Internet
Author: User

JS in the function of the process is not just a simple local variable overrides global variables, and functions inside the declaration of the situation. Like what:

<script>      var a =1;      function test () {          alert (a);//a is undefined! this is not a global variable because a local variable with the same name is already declared in function scope (the 4th line of the functional body),                       // So the global variable A is overwritten, which means that JavaScript performs a full analysis of the entire script file's definition before execution, so the                       variable A in the//function body is pointed to the internal local variable before the function test () is executed. Instead of pointing to the external global variable. But at this point a only declaration, has not been assigned value, so output undefined.          a=4                 Alert (a);  A is 4, no suspense, right? Here's a or a local variable Oh!          var A;     Local variable a declares          alert (a) in this line;  A is still 4, because the previous 4 has been assigned to a.      }      Test ();      alert (a); A is 1, and here is not within function scope, the value of a is the value of the global variable  </script>  

JavaScript about local variables and global variables

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.