in JavaScript by
Q:
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

► Click here to show 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.

JavaScript
Learn More with Madanswer

Related questions

0 votes
asked Mar 21 in JavaScript by rajeshsharma
...