setTimeout + closures

function x() {
    var i = 1;
    setTimeout(function () {
        console.log(i);
    }, 3000);
    console.log('shreyash');
}
x();
    This function form a closure and remember this reference of i . whenever it function goes it takes the reference of i along with it. what setTimeout do is? it takes the callback function and store into some place and attach timer to it. js not wait for it so it goes to next line and print shreyash on console.
and once the timer expire it take that function and puts to the callstack and run it.

Print 1–5 each an every sec.
for (let i = 0; i <= 5; i++{
    setTimeout(function () {
        console.log(i)
    }, i * 1000)
}
0
1
2
3
4
5