5

On the current Google Chrome (Version 22.0.1229.79, on an iMac with Mountain Lion), the following code

var arr = [1, 3, 5];
console.log(arr);

delete arr[1];
console.log(arr);

console.log(arr.pop());
console.log(arr);

will show

[1, undefined × 2] 
[1, undefined × 2] 
5 
[1, undefined × 1] 

there are also other situation that caused Firefox to behave similarly as well. Are they bugs on Chrome and Firefox -- but it would seem strange that both Firefox and Chrome are susceptible to similar bugs -- or is it some behavior with array delete and console.log? Supposedly, console.log should not be running on a separate thread.

nonopolarity
  • 146,324
  • 131
  • 460
  • 740

2 Answers2

1

In Firefox 7.0:

var arr = [1,3,5];

console.log(delete arr[1]); // will show [1, undefined, 5]

And in my opinion it's a correct behavior =) So may be it's just a bug.

XiM
  • 396
  • 3
  • 15
  • 2
    The op is not asking why the result is `[1, undefined, 5]`. – xdazz Oct 07 '12 at 08:19
  • a similar bug showed up on Firefox 15.0.1 but it involved 10 tests for `arr1` to `arr10`, and if I removed the test for `arr9`, then the output became "correct", and if I removed the test for `arr1` to `arr8`, the output became "correct" again. `arr10` is totally independent of other array tests, so it is similar to what Chrome was showing in the original question – nonopolarity Oct 07 '12 at 08:20
  • So `delete` is working correct and `console.log` isn't. If you do not re-define the `console.log`, I would suggest that this is simply a bug of browsers. – XiM Oct 07 '12 at 08:44
1

It is due to queued up console.log processing, so the printing is delayed, and it shows a later version of the object or array: Is Chrome's JavaScript console lazy about evaluating arrays?

My answer there has 5 solutions and JSON.stringify() was the best one.

Community
  • 1
  • 1
nonopolarity
  • 146,324
  • 131
  • 460
  • 740