Js global variables, local variables, and scope

Source: Internet
Author: User

<Script type = "text/javascript"> var a = 100; function test () {var B = 2 * a; var a = 200; var c = a/2; alert (B); alert (c) ;}test (); </script> <br/> <center> If the effect cannot be displayed, press Ctrl + F5 to refresh the page, more webpage code: <a href = 'HTTP: // www.bkjia.com/'target = '_ blank'> http://www.bkjia.com/</a> </center>

Tip: the code can be modified before running!

Running result: NaN, 100

Reference:

1. Global variables and their scopes
Global variables refer to the amount of definitions and descriptions in the description section at the beginning of the program. It has two scopes:
(1) When the global variables and local variables have different names, their scope is the whole program.
(2) When global variables and local variables have the same name, the scope of global variables does not include the scope of local variables with the same name.
2. Local variables and their scopes
All variables used in the subroutine must be included in the subroutine. The variables described in the subroutine are called local variables. The scope of a local variable is its subroutine. Format parameters can only be valid in subprograms. Therefore, it is also a local variable. The scope of local variables can be divided into two situations:
(1) When the local variable names of the outer program are different from those of the local variables in the nesting process, the local variable scope of the outer process contains nested procedures.
(2) When the local variable name in the outer process and the local variable name in the nested process have the same name, the scope of the local variable name in the outer process does not include this process.

Source: www.putaoshu.com

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.