Curious to know step by step Process of for loop which involves let and var


Ask A Question





Login To post a question

I have 2 simple code snippet about for loop involving let and var separately.
First code which has a variable declared with let

 for (let i = 0; i < 10; i++) {
       setTimeout(function() {
          console.log(i);
       }, 1000);
    }

so it will show o/p like this

0123456789 

but if I replace let with var like this

 for (var i = 0; i < 10; i++) {
       setTimeout(function() {
          console.log(i);
       }, 1000);
    }
it will print 10 for ten times. 

I know it is something related to function level scope and block-level scope, but want to clearly understand step by step process of execution.

Thanks in advance.

For more info please click herehttps://stackoverflow.com/q/55780770



Have a question or need a custom quote?
info@smartinfocare.com