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.

5
  • As a workaround print: arr.toString() Commented Oct 7, 2012 at 8:10
  • do you know why a workaround is preferred? Commented Oct 7, 2012 at 8:13
  • 1
    possible duplicate of Is Chrome's JavaScript console lazy about evaluating arrays? Commented Oct 7, 2012 at 9:13
  • @動靜能量: Seems like a bug that was done by the same person. FF14 is fine, while FF15 is not. Vandalized? Commented Oct 7, 2012 at 9:29
  • dup of stackoverflow.com/q/4057440/5849 Commented Oct 9, 2012 at 16:15

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The op is not asking why the result is [1, undefined, 5].
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
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.