Skip to content Skip to sidebar Skip to footer

Javascript Unexpected Console Output With Array Assignment;

I am getting unexpected console output after array assignment in webkit browsers (Chrome 16.0.912.77 and Safari 5.1.2 - 7534.52.7). Here is my function that demonstrates the error:

Solution 1:

This is because console.log is by reference and asynchronous, and your push() ends up executing before the result is displayed.

You could do a quick:

console.log(myArray.slice());

Instead, for debugging purposes.


To test this more obviously:

var a = []; console.log(a); a.push(1, 2, 3, 4, 5);

will give [1, 2, 3, 4, 5].

var a = []; console.log(a); setTimeout(function() {a.push(1, 2, 3, 4, 5);}, t);

gives the wrong result for me with t = 5 and the right result for t = 100.

Post a Comment for "Javascript Unexpected Console Output With Array Assignment;"