Wrong value in console.log [duplicate]

It’s because the log is delayed until Chrome has time to do it (i.e. your scripts releases the CPU).

Try this to understand what happens :

var t=[0,2];
console.log(t);
setTimeout(function() {
     t[0]+=2;
   console.log(t);
}, 1000);

It outputs what you expect.

Is that a bug of Chrome ? Maybe a side effect of an optimization. At least it’s a dangerous design…

Why is there a difference ? I suppose Chrome stores temporarily what it must log, as a primary (immutable) value in the first case, as a pointer to the array in the last case.

Leave a Comment