9

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.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Mat Richardson
  • 3,576
  • 4
  • 31
  • 56
  • can u show the full code because ++a; and a++; are same if they are individual statement and not composite with other statement. – Romil Kumar Jain Jun 26 '12 at 16:14
  • This is the full code - I'm using an example from a book and trying to understand what's going on. I'm testing it out using the console windows of both firefox and chrome. Results are the same either way. – Mat Richardson Jun 26 '12 at 16:16
  • 2
    The second example prints 1 through 6 -> http://jsfiddle.net/USYSH/ – Manse Jun 26 '12 at 16:16
  • 1
    possible duplicate of [What's the difference between ++i and i++ in JavaScript](http://stackoverflow.com/questions/6378646/whats-the-difference-between-i-and-i-in-javascript) – epascarello Jun 26 '12 at 16:17
  • @MatRichardson, did u actually executed them or following the answer of book. – Romil Kumar Jain Jun 26 '12 at 16:18
  • Your code works fine in jsFiddle but when I run using the firebug console I get different results. Very strange.. – Mat Richardson Jun 26 '12 at 16:18
  • 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 Jun 26 '12 at 16:18
  • 1
    @epascarello this is not the same question as that. It's more about confusion over the console output. – tybro0103 Jun 26 '12 at 16:31

3 Answers3

14

The 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.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 1
    That doesn't make sense to me at all. If the value was 7, why is it getting past the initial condition (ie a<=6)...to my mind that shouldn't happen. – Mat Richardson Jun 26 '12 at 16:15
  • 2
    It's not getting past the initial condition. It just happens that the last statement executed is `++a;` so that gets printed "for free" by the console. – Pointy Jun 26 '12 at 16:16
  • 1
    @Romil sorry, but that statement is incorrect. They're not the same in the context of the browser JavaScript console, which always prints out the result value of the statement. Try it yourself from your browser right now. – Pointy Jun 26 '12 at 16:17
  • Pointy is right, try it in the console. `++a` prints 1,2,3,4,5,6,7 while `a++` prints 1,2,3,4,5,6,6 – ohaal Jun 26 '12 at 16:19
  • 2
    AH!! Penny drops...The browser always prints out the final value. Thank you - that's made it suddenly very clear and I'm glad I'm not cracking up. – Mat Richardson Jun 26 '12 at 16:19
  • 1
    For anyone interested, [here's a question](http://stackoverflow.com/questions/8618270/using-a-blocks-return-value-in-javascript) that deals with the "return" value of statement blocks. *(Only observable with `eval`.)* http://jsfiddle.net/gNfCV/ –  Jun 26 '12 at 17:01
2

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.

tybro0103
  • 48,327
  • 33
  • 144
  • 170
0

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.

Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
  • Probably should read through the comments. OP is asking about the observed output in the `console`, which will usually include `7` for the second code example. –  Jun 26 '12 at 17:06
  • 1
    Please check the http://jsfiddle.net/eqA8t/ . I too checked in the console.. It returns 6 only. :):) – Nishu Tayal Jun 26 '12 at 17:08
  • What the jsfiddle shows doesn't matter since that's not what OP is asking about. If you type the code directly into the console, it'll usually include the `7`. *(I say "usually" because there may be some console out there that doesn't do this.)* The question wasn't entirely clear on that, but the comments cleared it up. –  Jun 26 '12 at 17:13