0 votes
in JavaScript by
Which of the following values will be returned by the last statement in the given code?

functionconstfun()  

{  

var fun =[];  

for(vari=0;i<10;i++)  

        fun[i]=function(){returni;};  

return fun;  

}  

var fun =constfun();  

fun[5]()  

a) 10

b) 12

c) 8

d) 9

1 Answer

0 votes
by

Answer: C

Reason: The code given in the following question creates at least 10 closures, stores them as an array.Closures are all specified within the same function invocation, so they share access to the variable i.At the time of "constfun()" method returns 10 as the value of the variable i, all closures shares this value. So, all functions in a given array of functions return the exact same value.

...