4

I'm getting different results for this fiddle on Google Chrome (14.0.835.186) and Firefox (6.0.2).

Can anyone explain the discrepancy? What is the behaviour determined by the specifications?

EDIT: On Firefox I see [0], [0, 1], etc. On Chrome I see [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], etc.

I'm using Mac OS 10.6.8.

Randomblue
  • 112,777
  • 145
  • 353
  • 547
  • Looks like it has been reported as a bug: https://bugs.webkit.org/show_bug.cgi?id=35801 – Jacob Sep 30 '11 at 22:25
  • To get around it, you could do `var a = []; console.log(a.join(',')); a[0] = 1; console.log(a.join(','));` – timrwood Sep 30 '11 at 23:46

3 Answers3

3

Firefox is more technically correct in this case as it outputs the state of the object at each point in the loop whereas Chrome is apparently waiting until the end of the loop to output each console.log, but I'm not aware of a standards specification that covers the console host object.

See this jsFiddle: http://jsfiddle.net/jfriend00/LRGP2/ to show that this is only console.log that has this odd behavior.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Why the downvote? I don't think you're correct here. This is a peculiarity of console.log and doesn't happen to other functions placed there. See this jsFiddle http://jsfiddle.net/jfriend00/LRGP2/ with a different function in the loop and it works as expected. – jfriend00 Sep 30 '11 at 22:17
  • Yeah, I spoke too fast. It's a `console.log` issue, which makes debugging more challenging. Edit your answer, and I'll upvote. – Randomblue Sep 30 '11 at 22:17
  • I added the jsFiddle to my answer. – jfriend00 Sep 30 '11 at 22:19
2

See:

It's an odd behavior or the console, though I can't tell you why.

Edit: Just to make sure it's clear, this is a 'bug' in the console only, there is no problem with the way the arrays are created in Chrome.

Community
  • 1
  • 1
Levi Morrison
  • 19,116
  • 7
  • 65
  • 85
2

You are logging a live object.

Try the code below (fiddle) and see the difference:

var i, test = [];

for(i=0; i<5; i++) {
    test.push(i);
    console.log( test.toString() ); // notice .toString() addition
}

Btw, same and aggravated example can be seen in Opera Dragongfly - arrays are even clickable and expandable there.

c69
  • 19,951
  • 7
  • 52
  • 82