+ 2

javascript question 2 explication needed

the code: for( var j=0, i=4, j<5, j++) { ( function (i) { setTimeout( function () { console.log(i); }, 1000) }) (J) } the output is: 01234 can anyone explain how this code function?

8th Jul 2018, 7:44 PM
Mr. Jami
Mr. Jami - avatar
3 Answers
+ 2
The code you written contains many syntax errors. (J) is different from (j). Loop statements/expressions are seperated with semicolons, not commas. I will assume that it is written correctly. (function(i){//do something with i})(j) is a self-invoking function, with i as the parameter name defined along with the function, while j is the argument passed to the function. The loop will output the value of j each iteration.
8th Jul 2018, 11:05 PM
HoĂ ng Nguyễn Văn
HoĂ ng Nguyễn Văn - avatar