A question of JavaScript about variable scope

Source: Internet
Author: User
Tags variable scope

Shinejaie Original, reproduced please indicate the source.

Last night in an Exchange group saw a netizen raised a question to ask questions for help. Just as I saw it, I thought about it and thought it would be helpful to understand the scope of JavaScript, so I tried to sort out my own problem-solving ideas and hope to help others.

First look at the following questions:
1     vararr = [1, 2, 3];2      for(vari = 0, J; j = arr[i++];) {3 Console.log (j);4     }5 6Console.log ('---------');7 Console.log (i);8Console.log ('---------');9 Console.log (j);TenConsole.log ('---------');

Before solving the problem, let's review the knowledge of the variable domain in JavaScript.

Global Variables (GLOBALS)

Global variables are variables that can be accessed anywhere, in two cases

    • Declare outside the function, whether or not to use the var keyword
    • Declare in function, do not use the var keyword, the statement of course must be executed to
Locals (local )Local variables can only be accessed within the declared function
    • Declare in function, use the var keyword

Two points to note the place

Look at the code first:

1 // output undefined 2 3  for (var i = 0; i < 1; i++) {}; 4 5 // Output 1
    • JavaScript does not exist in the statement scope , the variables defined within the statement will spread to the outside of the statement, in the example I declared in the for statement, but can be accessed outside the For statement
    • I can be accessed before the For statement, except that it is not yet assigned

Start our problem solving.

i++ is added after I use:

First execution, J=arr[0], after I=1,console.log (j) Output 1

Second execution, J=arr[1], after I=2,ocnsole.log (j) Output 2

Third execution, J=arr[2], after I=3,ocnsole.log (j) Output 3

Fourth time (not conforming to condition), j=arr[3] is undefined, then I=4,ocnsole.log (j) has no output, exits for loop

After the For statement is executed, the output 4,console.log (j) Output is Console.log (i) from the above analysis undefined

The final output is:

123---------4---------undefined---------

According to the above analysis and results, we must have made clear, and then we began to extrapolate it.

To change a question by a question

Topic:

1     vararr = [1, 2, 3];2      for(vari = 0, J; j = arr[++i];) {3 Console.log (j);4     }5 6Console.log ('---------');7 Console.log (i);8Console.log ('---------');9 Console.log (j);TenConsole.log ('---------');

Answer:

1 2 2 3 3 --------- 4 3 5 --------- 6 undefined 7 ---------
To change the question by two

Topic:

1     functionxxx () {2         vararr = [1, 2, 3];3          for(vari = 0, J; j = arr[i++];) {4 Console.log (j);5         }6     }7 xxx ();8 9Console.log ('---------');Ten Console.log (i); OneConsole.log ('---------'); A Console.log (j); -Console.log ('---------');

Answer:

123--------- Error: uncaught referenceerror:i is not defined

A question of JavaScript about variable scope

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.