An interview question about the scope of JavaScript variables and a question about javascript Variables
I think this question is very helpful for understanding the JavaScript scope. I want to sort out my problem-solving ideas again, hoping to help others.
First, let's take a 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 solving the problem, let's review the knowledge about variable fields in JavaScript.
Global)
Global variables are variables that can be accessed anywhere.
Declare outside the function, whether or not the var keyword is used
Declare in the function without using the var keyword. Of course, the declared statement must be executed.
Local)
Local variables can only be accessed within the declared function
Declare in the function and use the var keyword
Two notes
First look at the Code:
Alert (I); // output undefined for (var I = 0; I <1; I ++) {}; alert (I); // output 1
JavaScript does not have the statement scope. variables defined in the statement will spread out of the statement. In this example, I declare the variables in the for statement, but can still be accessed outside the for statement.
I can be accessed before the for statement, but it is not assigned a value yet.
Start solving problems
I ++ is added after I is used:
For the first execution, j = arr [0], then I = 1, console. log (j) Outputs 1
For the second execution, j = arr [1], then I = 2, ocnsole. log (j) outputs 2
In the third execution, j = arr [2], then I = 3, ocnsole. log (j) outputs 3
The fourth (not compliant with the for condition), j = arr [3] is undefined, and then I = 4, ocnsole. log (j) has no output and exits the for loop.
After the for statement is executed, the console. log (I) analysis shows that the output 4 and the console. log (j) Output undefined.
The output result is as follows:
2------------------undefined---------
We have already figured out the above analysis and results, so let's start to draw a line between them.
Change Question 1 by question
Question:
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:
23---------3---------undefined---------
Change Question 2 by question
Question:
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:
123 --------- error: Uncaught ReferenceError: I is not defined
I would like to share this with you and hope to help you understand the JavaScript scope.
Articles you may be interested in:
- Discussion on js variable scope and accessibility
- Variable scope in javascript callback Functions
- Easier understanding of JavaScript variable scopes
- In-depth understanding of JavaScript variable scopes
- Inference on the scope of variables declared by var in JavaScript
- Discussion about js variable scope and this pointer