Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Array delete or pop causing race condition with console.log?

Tags:

javascript

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.

like image 860
nonopolarity Avatar asked Nov 04 '22 14:11

nonopolarity


2 Answers

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.

like image 103
XiM Avatar answered Nov 15 '22 04:11

XiM


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.

like image 45
nonopolarity Avatar answered Nov 15 '22 06:11

nonopolarity