Home  >  Article  >  Web Front-end  >  An interview question about JavaScript variable scope_javascript skills

An interview question about JavaScript variable scope_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:11:292025browse

The editor feels that this question is very helpful for understanding JavaScript scope, so I have sorted out my own problem-solving ideas again, hoping to be helpful to others.

First look at the interview 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 solving the problem, let us first review the knowledge related to variable fields in JavaScript.

Global variables (Global)
Global variables refer to variables that can be accessed anywhere. There are two situations

Declared outside function, whether using the var keyword or not
Declare it in function without using the var keyword. Of course, the declared statement must be executed
Local variable (Local)
Local variables can only be accessed inside the declared function
Declared inside function, use var keyword
Two points to note

Look at the code first:

alert(i); // 输出 undefined
 
 for (var i = 0; i < 1; i++){};
 
 alert(i); // 输出1

JavaScript does not have statement scope. Variables defined within the statement will spread outside the statement. In the example, i is declared in the for statement, but it can still be accessed outside the for statement
i can be accessed before the for statement, but it has not been assigned yet
Let’s start our problem solving

i++ is added after i is used:

When executed for the first time, j=arr[0], then i=1, console.log(j) outputs 1

The second time it is executed, j=arr[1], then i=2, ocnsole.log(j) outputs 2

The third time it is executed, j=arr[2], then i=3, ocnsole.log(j) outputs 3

The fourth time (does not meet the for condition), j=arr[3] is undefined, then i=4, ocnsole.log(j) has no output, and exits the for loop

After the for statement is executed, console.log(i) outputs 4 from the above analysis, and console.log(j) outputs undefined

The final output result is:

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

Everyone must have understood the above analysis and results, and then we can start to draw inferences.

Borrowing a question and changing the question one
Title:
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
---------

Borrowed question and revised question 2
Title:

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
---------
报错:Uncaught ReferenceError: i is not defined

I’m sharing this with you, I hope it will be helpful for you to understand JavaScript scope.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn