weird array behaviour in javascript [duplicate]
This behaviour isn't limited to arrays, any object behaves this way in the console
What you are seeing is the result of console in all browsers "lying" to you
if you console.log(anyobject)
and inspect that object in the console, what you will see is current anyobject
- not what it was when console.log
was executed
var obj = {}
console.log(obj);
obj.test = 1;
var arr = [1];
console.log(arr);
arr.push(2);
Now, if you open the developer console, click on the Object, you'll see test:1
Look at the array in the console - it is output as [1]
... yet, click on the array you see both elements
Note: chrome developer console does at least hint at the fact that it's lying to you - there's a blue i
, if you hover (or click, can't recall, don't use Chrome often enough) you'll see a message saying that the value shown is evaluated just now