JSFiddle - React, Tailwind, and code Playground

by Kaushik Ruparel

JavaScript

/*function closeOverMe() { var a=1;
return function() {
alert(a);     a+=1;
};
};
var witness = closeOverMe();
witness(); // closure witnessed!
witness(); // closure witnessed!

*/
//returns 3 because of closure

/*for (var i = 0; i < 3; i++) {
        setTimeout(function () {
            console.log(i);
        }, 1000 * i);

}*/

//solution without IIFE by passing param to setTimeout

for (var i = 0; i < 3; i++) {
    setTimeout(function (index) {
        console.log(index);
    }, 1000 * i, i);

}


//uses IIFE to get around sharing the same var

/*for (var i = 0; i < 3; i++) {
    (function (index) {
        setTimeout(function () {
            console.log(index);
        }, 1000 * index);
    })(i);

}*/