0

My code was compiled to the following, and it works, but I do not know why. The variable that references the incrementing value is not included as an argument in the for loop.

var _loop2 = function _loop2() {
 var p = document.createElement('p');
 p.setAttribute('class', 'location__result');
 p.setAttribute('data-id', response.features[i].id);
 p.setAttribute('data-center', response.features[i].center);
 p.textContent = response.features[i].place_name;
 p.addEventListener('click', function () {
 endingInput.value = p.textContent;
 endingResults.style.display = "none";
 placeIconAndZoom(p, position);
 });
 endingResults.appendChild(p);
};
for (var i = 0, length = response.features.length; i < length; i++) {
 _loop2();
}
asked Dec 13, 2019 at 19:21
17
  • 4
    Possible duplicate of: JavaScript loop variable scope Commented Dec 13, 2019 at 19:24
  • @Ruzihm I don't think this is a closure. I think @TylerRoper is right. The ideclaration is hoisted to the top of scope. Commented Dec 13, 2019 at 19:26
  • 1
    It is a closure, what else would it be? Commented Dec 13, 2019 at 19:26
  • @CodeBling I was mostly referring to the "simplest example of a closure" example here Commented Dec 13, 2019 at 19:27
  • 1
    What remains is, that the variable i is being found by _loop2, because it is in the closure of it. Commented Dec 13, 2019 at 19:49

2 Answers 2

0

A function (x) declared inside another function (y) has access to all variables in the scope of the x (unless they are masked by another variable of the same name).

y can also be the global or module scope rather than a function per se.

var i = "example";
function x() {
 console.log(i);
}
x();

answered Dec 13, 2019 at 19:24
Sign up to request clarification or add additional context in comments.

Comments

0

This is the classic case of why const and let were introduced in ES6

var has a function level scope. This differs to const and let in terms of scoping, where the variable defined in var can be accessible anywhere in hits function declaration.

In your case, we declare var i inside the for loop (the function level scope), and your for loop is calling your _loop2 function. Since _loop2 is part of the for loops function scope, you're able to access that variable.

This SO answer can explain it a million times better than I can: What's the difference between using "let" and "var"?

You can fix this by either adding: use 'strict' ontop of your script file, but a much better way is to avoid using var all together (unless there is a special circumstance) and switch it to let

Shiny
5,0953 gold badges19 silver badges33 bronze badges
answered Dec 13, 2019 at 19:24

3 Comments

"var is a global scope" — No, it isn't. It is function level scope, not global. "use strict" (you misplaced the quotes) has no effect on variables declared with var. You are confusing it with global variables that are declared without the use of any scope keyword (var, let, const).
Sorry let me edit my answer to be more specific, thanks everyone
Re edit: "use strict" which you are still getting the syntax for wrong will still not make a difference here.