Can any one explane why it was returning output as 3


var printNumTwo;
for (var i = 0; i < 3; i++) {
if (i === 2) {
printNumTwo = function() {
return i;

i think its because you are using var i =0;
instead of let because var is function scoped and let is block scoped

i think it will be helpfull to get familiar with scopes and hoisting ,this article might help: Var, Let, and Const – What's the Difference?