1

I'm using Firefox's Web Console (FF v22). Using console.info(), future changes to an array are reflected. Is this a bug with the Web Console? Or does JavaScript on FF behave like this?

For example:

var myArr = [1];
console.info(myArr) // on Firefox [1,2] - NOT EXPECTED
myArr.push(2);
console.info(myArr) // on Firefox [1,2] - EXPECTED

IE on the other hand does behave as expected.

var myArr = [1];
console.info(myArr) // on IE: 1
myArr.push(2);
console.info(myArr) // on IE: 1,2
Norman Cousineau
  • 327
  • 1
  • 3
  • 15
  • 1
    possible duplicate of [Is Chrome's JavaScript console lazy about evaluating arrays?](http://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) – epascarello Jul 18 '13 at 15:11

1 Answers1

2

This happens because the console.info call is asynchronous. It may not finish before lines after it have completed and as you are dealing with an array which is passed by reference, the console.info call received a pointer to the array (which seems to have been updated before the log was made) rather than the value at the time console.info was called.

Smern
  • 18,746
  • 21
  • 72
  • 90
  • Asynchronous explains it, so this answer adds to post 'Is Chrome's JavaScript console lazy about evaluating arrays?'. As it points out, this is worked around by calling the toString() method on the array. (Sorry for duplicate question, but I wasn't sure how to word the search for an answer.) – Norman Cousineau Jul 18 '13 at 15:29