Why are these loop results inconsistent?

I tried rewriting the same loop for both a for loop and a while loop. The for loop will print my lines to the screen, but the while loop does not. Why would they not work the same? What would I need to do to the while loop to make it have the same behavior as the for loop?


var write_this = '\
';
var i = 0;
while (i < 3) {
   write_this += 'Does not print to console three times.\
';
   i++;
}


var write_this = '\
';
for (i = 0; i < 3; i++) {
      write_this += 'Prints to console three times.\
';
}

var write_this = '\
';
var i = 0;
while (i < 3) {
   write_this += 'Does not print to console three times.\
';
   i++;
}
console.log (write_this);

Works fine for me. Maybe it’s something else in your script?

RLM

I’m using these with the Firebug console. Could it be a Firebug thing?

No, likewise using firebug. I’d check the rest of your script, even your script tags.

RLM

Well, there is no actual script. I’m just playing with the different loops. I typed the first loop into Firebug and ran it exactly as I listed it above, and the Firebug console just printed this:

>>> var write_this = ’
'; var i = 0; while (i < 3) … not print to console three times.
'; i++; }
2

(Not sure where the “2” is coming from)

Then I clear the while loop and run the for loop all by itself and Firebug gives me:

>>> var write_this = ’
'; for (i = 0; i < 3; i++) {…te_this += 'Prints to console three times.
'; }
"
Prints to console three times.
Prints to console three times.
Prints to console three times.
"

What I’m trying to figure out is why the while loop won’t print the 3 lines like the for loop does. I’m guessing it must be printing them out somewhere if it’s working for you, but I wonder why my Firebug is acting differently?