A _javascript technique for face test questions about JavaScript variable scopes

Source: Internet
Author: User

Small knitting think this problem to understand JavaScript scope still very helpful, hereby also put their own problem-solving ideas combed again, hope to others help.

First look at the following questions:

var arr = [1, 2, 3];
  for (var i = 0, j; j = arr[i++];) {
    Console.log (j);
  }

  Console.log ('---------');
  Console.log (i);
  Console.log ('---------');
  Console.log (j);
  Console.log ('---------');

Before we solve the problem, we'll review the knowledge about variable fields in JavaScript.

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

Declaration outside function, whether or not with the VAR keyword
In the function declaration, do not use the var keyword, of course, declared statements must be executed in order to
Locals (local)
Local variables can only be accessed within the declared function
Declare in function, using the var keyword
Two points to pay attention to the place

First look at the code:

alert (i); Output undefined for
 
 (var i = 0; i < 1; i++) {};
 
 alert (i); Output 1

JavaScript does not exist in the statement scope, the variables defined within the statement are spread outside the statement, in the example I declare in the FOR statement, but can be accessed outside of the for statement
I can be accessed before the For statement, except that it has not been assigned
Start our problem solving.

The i++ is added after I use:

First time 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 time, j=arr[2], after I=3,ocnsole.log (j) Output 3

The fourth time (does not conform to the for condition), j=arr[3] is undefined, after I=4,ocnsole.log (j) does not have the output, exits the For loop

After the end of the For statement is executed, Console.log (i) shows the output 4,console.log (j) Output undefined

The final output is:

2
---------
---------
undefined
---------

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

Borrow a question to change the title
Topic:
var arr = [1, 2, 3];

  for (var i = 0, j; j = Arr[++i];) {
    Console.log (j);
  }

  Console.log ('---------');
  Console.log (i);
  Console.log ('---------');
  Console.log (j);
  Console.log ('---------');

Answer:

2
3
---------
3
---------
undefined
---------

Two questions to change the title
Topic:

function xxx () {
    var arr = [1, 2, 3];
    for (var i = 0, j; j = arr[i++];) {
      Console.log (j);
    }
  }
  xxx ();

  Console.log ('---------');
  Console.log (i);
  Console.log ('---------');
  Console.log (j);
  Console.log ('---------');

Answer:

1
2
3
---------
error: uncaught referenceerror:i is not defined

For everyone to share this, I hope to understand the JavaScript scope to help.

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.