Var in For Loops: Understanding Async Code Behavior

Var in for loops part 2 Did you Know actual flow of code ? for (var i = 0; i < 3; i++) {  setTimeout(function() {   console.log(i);  }, 1000); } Var in for loop actually prints 0, 1, 2 but because of async code waits in Macrotask with 3 times and when come with for execution it picks latest loop value thats why it prints same value 3 , 3 , 3 #React #ReactJS #ReactDeveloper #FrontendDevelopment #JavaScript #WebDevelopment #FrontendEngineer #UIEngineering #ComponentBased #Hooks #NextJS #SinglePageApplications #WebApps #SoftwareEngineering #CleanCode #DevCommunity #BuildInPublic #Coding #TechCareers #DeveloperLife #Javascript

  • text

To view or add a comment, sign in

Explore content categories