I don't understand this challenge in Javascript. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

I don't understand this challenge in Javascript.

I don't get why the output of the following code in JavaScript is 333. Could anyone explain it please? for(var i=0; i<3;i++){ setTimeout(function(){ document.write(i);} ,200); }

1st Oct 2017, 1:05 PM
Vahid
Vahid - avatar
4 Answers
+ 4
maybe Timeout capture last value of i, and execute 3 times, because the "for" is executed before of timeout function
1st Oct 2017, 2:00 PM
Geiner Salcedo Salgado
Geiner Salcedo Salgado - avatar
+ 2
That's what I thought but I am not sure
1st Oct 2017, 2:20 PM
Vahid
Vahid - avatar
+ 2
I tried it in google chrom it seems your guess is accurate. But it is very strange.
1st Oct 2017, 2:28 PM
Vahid
Vahid - avatar