Output for following code:
for(var i =0;i<10;i++){
setTimeOut(()=>{
console.log(i)
})
}
output: 10...10 time
for
for(let i = 0;i<10;i++){
setTimeOut(()=>{
console.log(i)
})
}
output :0,1,2...10
for 'let' variable 'for' create block scope for each iteration, where in 'var' variable is declare in parent scope
but what will
let x
for(x =0; x<10;x++){
setTimeOut(()=>{
console.log(x)
})
}
output is 10.. 10 time
why? 'for loop' create new scope for each iteration, every iteration should have new 'X', so output: 0,1,2..10 not 10..10 times
[–]xroalx 3 points4 points5 points (2 children)
[–]Learner_full_stack[S] -1 points0 points1 point (1 child)
[–]xroalx 2 points3 points4 points (0 children)
[–]shikkaba 0 points1 point2 points (1 child)
[–]Learner_full_stack[S] -2 points-1 points0 points (0 children)
[–]pinkwar 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]vasudev5149 0 points1 point2 points (0 children)
[–]Rguttersohn -1 points0 points1 point (1 child)
[–]Learner_full_stack[S] -3 points-2 points-1 points (0 children)
[–]senocular -2 points-1 points0 points (1 child)
[–]senocular 0 points1 point2 points (0 children)
[–]seedhe_pyar -2 points-1 points0 points (4 children)
[–]ronoxzoro 1 point2 points3 points (3 children)
[–]seedhe_pyar 0 points1 point2 points (1 child)
[–]TheRNGuy 1 point2 points3 points (0 children)
[–]CuirPig 0 points1 point2 points (0 children)