What is a JavaScript Closure?
A JavaScript closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.
JavaScript Closures Inside Loops
When using closures inside loops, the value of the variable may not be what you expect. This is because the closure is created and evaluated each time the loop is executed, and each time, the value of the variable is the same—the value of the variable in the last iteration of the loop.
Let’s look at a simple example to illustrate this concept:
for (var i = 0; i < 5; i++) {
setTimeout(function() {
console.log(i);
}, 1000);}
In this example, we are using a for loop to create a setTimeout function five times. The setTimeout function will log the value of the variable i to the console after one second. You might expect the output to be 0, 1, 2, 3, 4, but the actual output is 5, 5, 5, 5, 5.
This is because the value of the variable i is the same in each iteration of the loop—the value of the variable in the last iteration of the loop. The closure has access to this variable, and it will keep its value even after the loop has finished executing.
Using Closures Inside Loops
To fix this issue, we can use an Immediately Invoked Function Expression (IIFE) to create a new scope for each iteration of the loop. This will ensure that the value of the variable is correct when the closure is executed.
for (var i = 0; i < 5; i++) {
(function(i) {
setTimeout(function() {
console.log(i);
}, 1000);
})(i);}
In this example, we are using an IIFE to create a new scope for each iteration of the loop. The IIFE takes the value of the variable i as an argument, and this value is passed to the setTimeout function. The output of this example is 0, 1, 2, 3, 4, as expected.
Summary
JavaScript closures inside loops can be tricky to understand, but with a little bit of practice, you can master them. Using an IIFE to create a new scope for each iteration of the loop is a simple and effective way to ensure that the value of the variable is correct when the closure is executed.


2 responses to “Understanding JavaScript Closures Inside Loops”
The example you hinted at with closures inside loops instantly made me think of the classic for-loop with var and setTimeout logging the same value multiple times. One thing I still find a bit tricky is reasoning about memory and performance: do many long-lived closures created inside large loops pose a real memory concern in modern engines, or is this mostly a theoretical worry? I am also curious how you personally decide between using an IIFE pattern versus simply using let in a loop when teaching closures to beginners… do you think understanding the IIFE version is still important, or is it more of a historical artifact now?
Ella, I really appreciate how you tied this to the classic var + setTimeout example and then pushed the question toward memory and teaching. In practice, long-lived closures are only a real memory concern if they accidentally capture large objects or DOM nodes that are no longer needed, so a good habit is to minimize what each closure closes over (e.g. pass only the data you need as arguments). When I teach, I start with let in loops for clarity, then briefly show the IIFE pattern so students can read older code and understand the underlying mechanism, rather than treating let as magic.