Can anyone explain why the following code works when run as part of function, but it produce strange result when run by itself in the Chrome Console window?
var foo = function() {
var x = 1;
while (x<3) {
console.log(x);
x = x+1;
}
}
foo(); // This prints 1,2 as expected
But when I run just while
part directly in Chrome Console I get 1,2,3 which makes no sense (see image for the output):
var y = 1;
while (y<3) {
console.log(y);
y = y+1;
}
// This prints 1,2,3 in the console
Note that there somewhat similar question about console.log
resulting in undefined
(Chrome/Firefox console.log always appends a line saying undefined), but there is no function call in my sample and while
does not ever return any value.