0

What will be the output of the console.log function and how?

Note: if your answer is 2 can you explain to me how and why not zero logically it should return zero from the array. var funcArray = []; for ( var i = 0; i < 2; i++ ){ funcArray.push( function(){ return i } ) } console.log( funcArray[0]() );

5th Aug 2018, 2:33 AM
anjit pariyar
anjit pariyar - avatar
4 Answers
0
why zero? Every array element is actually a function which returns the value of I and you see, what's the value of i there after the for loop... it's 2 isn't it! That's why every function will return the same and that is 2 here.
5th Aug 2018, 5:27 PM
ŠØŠ°Ń‰Šø Š Š°Š½Š¶Š°Š½
ŠØŠ°Ń‰Šø Š Š°Š½Š¶Š°Š½ - avatar
+ 1
console.log() takes almost anything as input and outputs in the console. To see it in working go to the js section and try writing: console.log("hello console");
5th Aug 2018, 6:00 AM
ŠØŠ°Ń‰Šø Š Š°Š½Š¶Š°Š½
ŠØŠ°Ń‰Šø Š Š°Š½Š¶Š°Š½ - avatar
+ 1
i appreciate it bro :)
5th Aug 2018, 5:30 PM
anjit pariyar
anjit pariyar - avatar
0
can you do this for me again :)
5th Aug 2018, 9:44 AM
anjit pariyar
anjit pariyar - avatar