I am new to JavaScript, and recently stumbled across this problem questioning what would be printed to the console. The correct answer is 4 4 4 4, because of pass-by-reference. However, I don't understand how this is the result. Why does pass-by-reference dictate the output if the output is simply a value of i that seems only dependent upon an iteration of a for-loop?
const array = [10, 21, 31, 41];
for(i = 0; i < array.length; i++) {
setTimeout(function print(array){
console.log(i)}, 3000);
}