Why use let in a for loop?

Do you know of any uses of using a new loop variable each time?

For each iteration of the loop a new variable with block level scope is declared. The for loop breaks down like this:

{ let i;
  i = 0;
  __status = {i};
}
{ let {i} = __status;
  if (i < 3)
           __status = {i};
}   { let {i} = __status;
      i++;
      if (i < 3)
          __status = {i};
    }   { let {i} = __status;
          i++;
      if(i<3){
 printNumTwo = function() {
      return i;
    };
}

//this is the i variable that printNumTwo will be binding to

i believe one answer to this is recursion.

if making a recursive call in the middle of some code that uses i both before and after the recursive call, a different i will be required on each loop for sure.

I couldn’t understand what you meant… could you provide a code example?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.