Why am I getting this result?

for (var i=1; i<=5; i++) {
 setTimeout( ((i) => {
 return console.log( i );
 })(i), 1000 );
}

Why do I get here 1 2 3 4 5 0?
Where 0?
And why are all displayed without delay?
April 7th 20 at 11:02
1 answer
April 7th 20 at 11:04
Solution
And why are all displayed without delay?

because you have ((i) => { return console.log( i ); })(i) sloveniaas function that executes immediately. make instead, ()=>{ return console.log( i ); }

and the whole example do this:
for (let i=1; i<=5; i++) {
 setTimeout( () => {
 return console.log( i );
 }, 1000 );
}


Note let instead of var in the ad cycle

Find more questions by tags JavaScript