LearnFrenzy
Sign in Take a free test

Closures · Closures MCQ · Question 6

Fixing the loop closure problem Answer, explanation & discussion

  • MCQ
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

Callbacks created in for (var i = 0; i < 3; i++) all print 3. What is the simplest fix?

  • A Use setTimeout with a delay of 0
  • B Declare the loop variable with let Correct
  • C Use an arrow function instead of a regular function
  • D Declare i with const outside the loop
Correct Answer: Option B

let gives each iteration its own binding. Arrow functions change this, not scope; const would fail on i++.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links