I have the following JavaScript code:
var counter = 0;
function printCounter(){
console.log("counter=" + ++counter);
setTimeout(printCounter, 1000);
}
printCounter();
I expect that it should print this output:
counter=1
counter=2
counter=3
...
But instead it prints following:
counter=1
undefined // <-- Notice this "undefined"
counter=2
counter=3
...
Why does it print "undefined" after the first iteration?
Important: I see such behavior only when the code executed in the JavaScript console. If it's the part of a page, it works fine.