re: 3 topics in 1 JavaScript interview question VIEW POST


I believe before the addition of let one way to solve this would be to wrap the call to setTimeout in an IIFE. I guess by immediately invoking the outer function with the current value of i it seals the value at that moment vs getting it from the global scope when the inner function is called.

  for (var i = 0; i < 3; i++) {
    (function(i) {
      setTimeout(function() {
      }, 1000)

// 0 1 2

code of conduct - report abuse