8

When running following code on console:

var counter=0; while(counter<5){ console.log(counter); counter++; }

console o\p: 0 1 2 3 4 4

whereas for following code works fine, without repeating last value:

for(var i=0; i<5; i++){ console.log(i); }

console o\p: 0 1 2 3 4

Now, if I place above for loop after above mentioned while loop , output is perfectly fine:

var counter=0; while(counter<5){ console.log(counter); counter++; } for(var i=0; i<5; i++){ console.log(i); }

console o\p: 0 1 2 3 4 0 1 2 3 4

whereas, if I place while loop after for loop , repetition of last number found.

for(var i=0; i<5; i++){ console.log(i); } var counter=0;while(counter<5){ console.log(counter); counter++; }

console o\p: 0 1 2 3 4 0 1 2 3 4 4

Request all to provide a reason on this unexpected behavior of while loop. Thanks.

Andrea Casaccia
  • 4,802
  • 4
  • 29
  • 54
Sarge
  • 178
  • 1
  • 1
  • 8
  • I don't think there is a problem see it working here http://jsfiddle.net/7p5L6kzz/ – depperm Jul 15 '15 at 15:43
  • 3
    last `4` in first case - just value for counter++ anfter loop, so it not a `console.log` output – Grundy Jul 15 '15 at 15:43
  • Looks totally fine for me - it is going through 0-4 (as you specified < 5) and then in the end it is printing the value of counter after the loop. That's where the double 4 is coming from in console. – Julia Will Jul 15 '15 at 15:44
  • 2
    Note that in the `for` loop version, instead of repeating the 4, you instead see `undefined` on the console - ie it's still displaying something "extra". – James Thorpe Jul 15 '15 at 15:47
  • @andrew thanks for reviewing my edit ;) I noticed when it was too late that it was wrong – Andrea Casaccia Jul 15 '15 at 15:49
  • @depperm : I am using firebug console. that must be due to different way of js engine implementation. – Sarge Jul 15 '15 at 15:56
  • @Grundy : Thanks for input, but why isn't happening when I am adding a for loop beneath while. – Sarge Jul 15 '15 at 15:57
  • @andrew : thx buddy, trying to start with JS :P – Sarge Jul 15 '15 at 16:02
  • 1
    as say @JamesThorpe: _instead of repeating the 4, you instead see `undefined` on the console_ – Grundy Jul 15 '15 at 16:06
  • I have forgotten the explanation why, but there is some sort of delay in ++ operators. For your first example, you can ensure this doesn't happen by doing the addition explicitly i.e. var counter=0; while(counter<5){ console.log(counter); counter = counter + 1; } – JsAndDotNet Jul 15 '15 at 16:29
  • 1
    @HockeyJ That simply changes it from displaying an extra 4 to displaying an "extra" 5. – James Thorpe Jul 15 '15 at 16:33
  • Thanks Grundy, James, Nit... Now I do have a reason for this abrupt behavior, well I tried this code on Chromium Version 37.0.2062.120 on Ubuntu 14.04 (281580) (64-bit) & Firebug 2.0.11 on Firefox 38.0, Mozilla Firefox for Ubuntu canonical - 1.0 and still cannot find that undefined on using for loop next to while. Thank you everyone for your support. – Sarge Jul 15 '15 at 16:42
  • @FelixKling : Buddy, why did you marked this question as duplicate. This question was asked in July 2015, and you marked it as a duplicate of a [question](http://stackoverflow.com/questions/35454291/javascript-while-loop-return-value) which was asked 8 months later i.e. Feb, 2016. – Sarge Mar 25 '16 at 08:01

2 Answers2

9

When performing operations in the console, the return value of the last executed line is always output.

That means that simply writing

var counter = 0; ++counter;

will log 1 to the console.

The same is happening in your loop, the return value of the last counter++ is output to the console as the value of the last executed expression.

Etheryte
  • 24,589
  • 11
  • 71
  • 116
  • 4
    In Chrome (not sure about other browsers), this is indicated by a small grey arrow. – James Thorpe Jul 15 '15 at 15:42
  • Thank you for your input, "he return value of the last executed line is always output." So why is it not happening when I am adding a for loop after while. – Sarge Jul 15 '15 at 15:51
  • 3
    @Sarge It _is_ happening, see my comment above on the question. In this case, the "return value of the last executed line" is `undefined`, which you _do_ see after the `for` loop version. – James Thorpe Jul 15 '15 at 15:54
2

The output of the log method depends on the javascript engine of the browser. The last value printed is not output of the loop itself.

Try: var counter=0; while(counter<5){ console.log(counter++); }

Dipen Shah
  • 25,562
  • 1
  • 32
  • 58