...

/

Solution Review: Closure, `setTimeout`, Scopes

Solution Review: Closure, `setTimeout`, Scopes

In this lesson, we will discuss the solutions to the questions in the previous lesson.

We'll cover the following...

Question: Solution review #

In the previous lesson, you were given the following code:

Press + to interact
const array = [5, 11, 18, 25];
for (var i = 0; i < array.length; i++) {
setTimeout(function() {
console.log('Element: ' + array[i] + ', at index: ' + i);
}, 3000);
}

For the code above, you were asked the following question:

Explanation #

Let’s start by running the code below:

Press + to interact
const array = [5, 11, 18, 25];
for (var i = 0; i < array.length; i++) {
setTimeout(function() {
console.log('Element: ' + array[i] + ', at index: ' + i);
}, 3000);
}

Before we get into the details of why the output ...