Using var within a loop:
| var i = 5; for (var i = 0; i < 10; i++) { // some code } // Here i is 10 |
Using let in a loop:
| let i = 5; for (let i = 0; i < 10; i++) { // some code } // Here i is 5 |
In the first example, using var, the loop variable redeclares the variable defined outside the loop.
In the second example, using let, the loop variable does not affect the outer variable and is only accessible within the loop.
The for/in loop and the for/of loop will be discussed in the next chapter.
The while loop and do/while loop will be covered in the upcoming chapters.