...
/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...
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 ...