0

Firstly, I have lines of code like this

var arr = [1];
console.log("first: ", arr);
arr.push(2);
console.log("second: ", arr);
arr.push(3);
console.log("third: ", arr);

and it outputs

first [1,2,3]
second [1,2,3]
third [1,2,3]

I have no idea why it is that.. Actually, it is a test that I attempt after having a lot of trouble with console.log and array on my project, it shows the array object with strange items that I never added to the array before (actually, after) but the array length is correct and if i check each item, it is also correct.. My friend explains me that Javascript executes the console.log "slower" than arr.push, is that true? Doesn't javascript follow the formal sequence code flow like other languages? And how can I prevent that? Console.log is very important for me and now I does not confident with its output anymore :(

Duc To
  • 333
  • 2
  • 6
  • Are your three `console.log` calls sequential? I have a feeling the code is not quite what you've given here. JS, even on webkit, does not do that unless the variable was already referenced, and that requires you to be out of scope. – Sébastien Renauld Apr 12 '13 at 10:00
  • 1
    @SébastienRenauld - I can confirm this behavior in Safari 5.1.7 on Windows. It prints `[1,2,3]` for all three `console.log` calls. Chrome 26, OTOH, prints the correct values. So it must be a bug in older versions of webkit that has been fixed? @OP, what browser are you testing in? – Michael Geary Apr 12 '13 at 10:15

0 Answers0