DEV Community

Discussion on: JavaScript Closure Simply Explained

Collapse
 
rctryoka profile image
Robert Carlo Tubig • Edited

in the interview question about closure
why does it shows a different result when you use let in the for loops ?

Collapse
 
salyadav profile image
Saloni Yadav

let is a block scope. so the value of i gets captured. in case of var its a global scope/scope of where the function is called. so when the function actually executes, the var i value is the last standing value, which is not the case with let.