for (var i = 0; i < 3; i++) {
const fun = () => console.log(i);
setTimeout(() => {
fun();
}, 1000);
}
I got this output: 3 3 3. This will print 3
three times.
From MDN:
The global
setTimeout()
method sets a timer which executes a function or specified piece of code once the timer expires.