function makeFunctionarray(){
const arr = [];
for(var i = 0 ; i < 5 ; i++){
arr.push(function() { console.log(i);});
};
return arr;
}
const output = makeFunctionarray();
output[0](); // so here output is 5
i am expecting output to 0 but here i get 5 so someone please explain me
jenovs
January 30, 2022, 9:34am
2
TL;DR - replace var
with let
.
thanks for response,
but i know that i can get 0 insted of 5 when i use let but i want to know what is happening under the hood .
why we get 5 when we use var ?
jenovs
January 30, 2022, 10:59am
4
system
Closed
July 31, 2022, 11:00pm
5
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.