Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Loop Scope

Using var within a loop:

Example

var i = 5;

for (var i = 0; i < 10; i++) {
  // some code
}

// Here i is 10

Using let in a loop:

Example

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.

For/Of and For/In Loops

The for/in loop and the for/of loop will be discussed in the next chapter.

While Loops

The while loop and do/while loop will be covered in the upcoming chapters.