1

enter image description here

Why does this while loop print "1" at the end?..I only want it to print console.log statement. Saw when using Codecademy.

for (i = 0; i < 2; i++) {
    console.log("I understand for loops twice..lol");
};

var whileUnderstand = 0;

while(whileUnderstand < 2) {
    console.log("I understand while loops twice..lol");
    whileUnderstand++;
}  

enter image description here

That question doesn't have the direct answers to the question that i asked. Furthermore, it only includes console.log statements instead of loops. Mainly, there are no answers saying that "The console is simply outputting the last evaluated value of the statement." which is the answer that solved my question.

Lavios
  • 1,169
  • 10
  • 22
  • @KushJain where is the rest of your code? – brso05 Jul 27 '15 at 14:18
  • @j08691 paste it in the console. – epascarello Jul 27 '15 at 14:20
  • Are you running this from your browsers console? When I try this from IE console it seems that it prints the value of the `++` statement before it is incremented. For example when I run `var a = 0; a++;` it prints `0`. – Ryan Rossiter Jul 27 '15 at 14:24
  • It's just the return value of that block of code, ran as if it were a function. Add another line at the end, after the while loop, that says `whileUnderstand++;` and you'll get 2 instead of 1. – Reinstate Monica Cellio Jul 27 '15 at 14:24
  • @B.Kemmer "why would it print '1' at the end?". That's literally the OP's question... – jmar777 Jul 27 '15 at 14:28
  • possible duplicate of [Chrome/Firefox console.log always prepends a line saying undefined](http://stackoverflow.com/questions/14633968/chrome-firefox-console-log-always-prepends-a-line-saying-undefined) – Hacketo Jul 27 '15 at 14:45
  • @Hacketo the question is probably close enough to be a duplicate, but I don't think the answer there is sufficient and/or correct. As we've discussed in the thread on Curt's answer, it's not as simple as "When you run any void function [...] it also prints out info about the return value". /2-cents – jmar777 Jul 27 '15 at 14:49

2 Answers2

7

This will only happen when running the code in a browser console.

It's caused by this line:

whileUnderstand++;

The console is simply outputting the last evaluated value of the statement.

enter image description here


The reason only 1 is logged and not also 0 is that outside of a console.log() call, only the last statement is logged.

For example if I have the following code snippet only "d" is logged:

var a = "a";
var b = "b";
a = "c";
b = "d";
Curtis
  • 101,612
  • 66
  • 270
  • 352
  • 1
    can you explain why it only seems to print `1`, and not `2` as well, though? Also, why does it seem to print the `1` *after* it's printed the intended `console.log('...')` statement both times? – jmar777 Jul 27 '15 at 14:20
  • 2
    'cause you use `whileUnderstand++`. It's postfix operator: it returns value before it was increased. Try prefix `++whileUnderstand` and you will get 2. – Ivan Jul 27 '15 at 14:22
  • 1
    Why does that only happen in browser console? – Lavios Jul 27 '15 at 14:24
  • @KushJain well where and why javascript would print this value ? console = input -> output (even if it's undefined) – Hacketo Jul 27 '15 at 14:26
  • @Lends No, I mean why does it only print the result of the increment operator once. That increment operator runs twice (and once before the second "I understand while loops...") log statement. I.e., what are the rules for when it does/doesn't log the result of an increment operation. – jmar777 Jul 27 '15 at 14:31
  • @Lends to your credit, though, if it is choosing to log the pre-incremented value, then my comment-question should be modified (i.e., why isn't it logging `0` and `1`, then?). – jmar777 Jul 27 '15 at 14:35
  • @jmar777 I believe only the last statement is logged. For example with the following code only `"d"` is logged: `var a = "a";var b = "b";a = "c";b = "d";` – Curtis Jul 27 '15 at 14:37
  • 1
    @jmar777 if you reverse the calls of `console.log` and `whileUnderstand++` in the while loop you get `undefined`. I guess console output the latest 'evaluated' value. – Hacketo Jul 27 '15 at 14:38
  • @Curt Yes, I believe you're right. Based on a few tests I just ran, the one important qualifier is that when determining the "last statement", it ignores expressions within `if()`, `while()`, and `for()` conditions (otherwise the OP's example code would have logged `false`, seeing as the last expression to actually run is `whileUnderstand < 2` (forgive me for conflating statement & expression there... that's probably part of the rationale too, really). – jmar777 Jul 27 '15 at 14:42
  • @Kush Jain, @jmar777 console is some kind of debugging tool. You can write `4+5` there and you will get `9` in your console. Use it inside script tag or somewhere else and you will get nothing. Console just tries to log last expression. So, whileUnderstand++ is just last there. Check this out: `var whileUnderstand = 0; var b = 10; while(whileUnderstand < 2 || 2 > 5) { console.log("I understand while loops twice..lol"); whileUnderstand++; b--; } ` You will get `9`. Which is `b--` on the last entry of while loop: `b--` (where b = 9) logs `9`. – Ivan Jul 27 '15 at 14:44
  • @Lends please see my previous comment. It's close to what you're describing, but it's not quite as simple as saying the "last expression" (seeing as, again, `whileUnderstand < 2` is technically the last expression). – jmar777 Jul 27 '15 at 14:45
  • @jmar777 it's last but it has no any meaning and sense for logging. It shows nothing. Every (except endless) loop ends with false value, so it gives nothing for debugging. I think that's the reason. – Ivan Jul 27 '15 at 14:58
  • @Lends I think that's valid rationale in most cases, but obviously JavaScript lets you do funky things like `if (x = getSomeTruthyOrFalsyValue()) {...}`, in which case it would certainly make sense to see the result. Either way, rationale aside, my comment was regarding behavior. – jmar777 Jul 27 '15 at 15:10
0
var whileUnderstand= 0;

while(whileUnderstand<2) {
    console.log("I understand while loops twice..lol");
    whileUnderstand++;
}  

Because it doesn't increment whileUnderstand when its equal to 2 if you want to print 2, you need to do this

var whileUnderstand= 0;

while(whileUnderstand<3) {
    console.log("I understand while loops twice..lol");
    whileUnderstand++;
}  
Huang Chen
  • 1,177
  • 9
  • 24