I am iterating an array iterator using for..of
and trying to log the value at a regular interval. My understanding is let
keyword will create new variable in each iteration(updated after mistake pointed by VLAZ in comment section) and inside the setIterval
it will get a new value. Instead of this , the log inside the setInterval is printing an inconsistent result which I am not able to understand the cause. Will appreciate if you can help to understand this.
Edit: I am aware setTimeout will log a consistent result , but I intended to know why setInterval is logging two result simultaneously and how it is deciding the values
const numbers = [1, 2, 3, 4, 5];
for (let [i, v] of numbers.entries()) {
setInterval(() => {
console.log('Output - ', v)
}, 2000 * (i + 1))
}