Can anyone please help me understand why the returned function is executed?
const arr = [10, 12, 15, 21];
for (var i = 0; i < arr.length; i++) {
setTimeout(function(i_local) {
//The function below is just returned and not called using '()', so why is it executed? Why does the console.log works?
return function() {
console.log('The index of this number is: ' + i_local);
}
}(i), 3000);
}