When you run the code directly in the browser console, it runs the code, then it registers the value of the last expressed expression, in this case the value is count++ , which is 9 at the final execution (it changes to 10 with the operator after the increment, i.e. after the value 9 "read").
If you changed your code to:
var count = 0; while (count < 10) { console.log(count); ++count; }
Instead, there will be log <- 10 .
But why doesn't the value return in every single loop?
Since it doesn't return at all, this is just console behavior.
Is there any way to catch this return value of a variable?
Yes, but you need to assign it:
var count = 0; var lastcount; while (count < 10) { console.log(count); lastcount = count++; } console.log(lastcount);
Note that if you run this snippet in the console, you will get two 9 in the log (one from the last loop, one from the additional console.log ), followed by <- undefined , because the last console.log returns undefined .
James thorpe
source share