Bizarre console.log behaviour in Chrome Developer Tools [duplicate]

Try this instead:

var a = []; console.log(a.toString()); a.push(1); console.log(a.toString());

It's not that the order of evaluation is strange, I bet, but that the conversion of the objects to printable form happens after the statements are all executed, at the point when Chrome is ready to actually dump out the log.


Same behavior here with Win7 on a x64 machine. My guess is that the log method holds a reference to a and queues the calls that happen to be in a single line.

EDIT It's not a Chrome/ium issue alone, I have witnessed the same with Firebug. As I said console logging must be queued in some ways.


At least with arrays, you can clone the array for each log call:

var a = [];console.log([].concat(a));a.push(1);console.log([].concat(a));

For objects, I recommend JSON:

var a = {};console.log(JSON.stringify(a));a[0]=1;console.log(JSON.stringify(a));

What's being "logged" is the object "a"... not the plain text representation of "a". The log display is clever enough to display a place holder for object "a", which gets filled in/populated "later" (seems like at the end of the event invocation). If you stick an alert() statement in you'll see the values you expect (logged values get "filled in"):

var a = [];
console.log(a);
alert('force console to display correctly');
a.push(1);
console.log(a);

This seems like a bug in Chrome to me (kinda goofy to have to put an alert statement in to see the correct logging information).

(note this question showed at the top of a google search for "console.log chrome only shows current values" so I thought I'd add my answer)


Yeah it does that on objects too....and if you change the value later (say, many seconds later) and then expand the object in the console, the new value will be in there. Weird, but can be useful in a sense.

If you want the current value, just say "console.log(a.toString());" or the like.