The scope of the var declaration is not just the for block but the current execution context, so all callback functions hold a reference to that same variable, ending up with the value after the last increment. To avoid this, you can either use let instead of var as let is indeed block scoped, or better yet, use forEach():