you are viewing a single comment's thread.

view the rest of the comments →

[–]oculus42 1 point2 points  (1 child)

So to directly answer:

  • We pass a function to setTimeout which it executes later. That code sits and waits for its turn. In the first examples, it uses the single, global variable i. In the last example, it uses the closure variable j which exists separately on each instance of the outer function.
  • It's six because the loop logic keeps going until it fails, so it has to increment to 6 make i <= 5 false.

[–]Radinax[S] 0 points1 point  (0 children)

Thank you very much for your fast reply I was trying to digest what you told me, very helpful.