I might be being a bit thicky here but please answer me this. Consider the following code:
a=1;
while(a<=6) {
console.log(a);
a++;
}
If I run this I get values in the console from 1 to 6, and then another 6.
Now look at this:
a=1;
while(a<=6) {
console.log(a);
++a;
}
Running this will now get me the values from 1 to 7.
Why is this happening? My understanding was that the statement block would only run if the expression evaluates to true. How can this be possible in the second of my examples? And why does 6 appear twice in the first? Very confusing for me.
If you can explain simply (I'm still learning) that would be great.
I might be being a bit thicky here but please answer me this. Consider the following code:
a=1;
while(a<=6) {
console.log(a);
a++;
}
If I run this I get values in the console from 1 to 6, and then another 6.
Now look at this:
a=1;
while(a<=6) {
console.log(a);
++a;
}
Running this will now get me the values from 1 to 7.
Why is this happening? My understanding was that the statement block would only run if the expression evaluates to true. How can this be possible in the second of my examples? And why does 6 appear twice in the first? Very confusing for me.
If you can explain simply (I'm still learning) that would be great.
Share Improve this question edited Jun 26, 2012 at 16:14 gen_Eric 227k42 gold badges303 silver badges342 bronze badges asked Jun 26, 2012 at 16:11 Mat RichardsonMat Richardson 3,6065 gold badges35 silver badges57 bronze badges 8- can u show the full code because ++a; and a++; are same if they are individual statement and not posite with other statement. – Romil Kumar Jain Commented Jun 26, 2012 at 16:14
- 2 The second example prints 1 through 6 -> jsfiddle/USYSH – Manse Commented Jun 26, 2012 at 16:16
- 1 possible duplicate of What's the difference between ++i and i++ in JavaScript – epascarello Commented Jun 26, 2012 at 16:17
-
3
@ManseUK not in Firefox it doesn't. The key ingredient is to do it from the browser JavaScript console, whose behavior is what's at issue here. The last number printed is not the result of a
console.log()
call. – Pointy Commented Jun 26, 2012 at 16:18 - 1 @epascarello this is not the same question as that. It's more about confusion over the console output. – tybro0103 Commented Jun 26, 2012 at 16:31
3 Answers
Reset to default 14The console prints for you the value of the last statement evaluated. In the second case, you pre-increment, so the value of that is 7 and not 6 as in the first one.
Change you console.log()
call to print more stuff and it'll be obvious:
console.log("a is: " + a);
You won't see that prefix on the last line.
In both cases, you're seeing an extra digit because the console is outputting the result of the last statement in the loop.
When that code is not executed directly in the console, you will not see what appears to be an extra digit.
See the fiddle with their response. Both return 1 to 6.
a++ : It returns the value of a before increment.
++a : It returns the value of a after increment.
Loops executes until value of 'a'<= 6.
When you run any code on console, it evalutes the variable value and prints its value also that's why you are getting one more 6
and 7
in the output.
No worries, when you'll run this code, will get the 1-6 values only.