This repository demonstrates a common JavaScript closure issue that arises when using setTimeout within a loop. The problem stems from how closures capture variables.
The bug.js file contains a function myFunction that uses a for loop and setTimeout to log the value of i after a delay. Due to how closures work in JavaScript, all the callbacks will log the final value of i (which is 10) instead of the value of i at the time the setTimeout was called.
The bugSolution.js file shows the solution to this problem. It uses an immediately invoked function expression (IIFE) to create a new scope for the variable i, effectively capturing its value correctly within each setTimeout callback.
- Clone this repository.
- Open both
bug.jsandbugSolution.jsfiles in your browser's developer console or a JavaScript environment. - Execute the code and observe the differences in output.