javascript - Why does console.log outside "for" loop print out an "extra" number? -
i'm working on simple javascript codes , there i'm not sure understand. when input code,
for (var x = 0; x < 5; x++) { console.log(x); }
naturally, prints out 0 4. if add
for (var x = 0; x < 5; x++) { console.log(x); } console.log(x);
then print out 0 5. why print out 5?
that's totally normal!
the last iteration not 5th 1 (which printed "4", expected), 6th one, found x
value ("5") exceeded defined limit.
since for
statement not closure, var x
stills exists @ loop end, prints value.
Comments
Post a Comment