Hi #dev. This is a very common JavaScript interview question: for (var i = 0; i < 3; i++) { setTimeout(() => console.log('var', i), 100); } for (let j = 0; j < 3; j++) { setTimeout(() => console.log('let', j), 100); } What will be the output of this code, and why? 💬 Drop your answer in the comments — let’s see how many of us get it right without running the code . #JavaScript #WebDevelopment #Frontend #Angular #InterviewQuestions #LearningTogether
var 3 var 3 var 3 let 0 let 1 let 2 All var outputs first (they were scheduled first) Then all let outputs
var — 3,3,3 let — 0,1,2 for let, everytime new ref value is stored
3,3,3 for var case 0,1,2 for let case
var3 var3 var3 let0 let1 let2
undefined/ null for all. They are added to macro queue and lose the context. Global context dont have wait. First i will be 3 because its a var. j will br null/undefined.