3

For example, I start with:

        var currentHistory = ['t1', 't2', 't3', 't4', 't5'];
        console.log(currentHistory);

I then swap an element and log again:

        var tmp = currentHistory[2];
        currentHistory[2] = currentHistory[0];
        currentHistory[0] = tmp;

        console.log(currentHistory);

Only to see that the output is the same in each case.

        Array[5] 't3', 't2', 't1', 't4', 't5'

        Array[5] 't3', 't2', 't1', 't4', 't5'

This inconsistency in space and time sent me quite mad last night and an answer would be appreciated.

Gga
  • 4,311
  • 14
  • 39
  • 74
  • possible duplicate of [Is Chrome's JavaScript console lazy about evaluating arrays?](http://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) – Felix Kling May 01 '14 at 12:14
  • 1
    I get ["t1", "t2", "t3", "t4", "t5"] then ["t3", "t2", "t1", "t4", "t5"] in chrome – GôTô May 01 '14 at 12:14

2 Answers2

3

Good question! Try this:

console.log(currentHistory.slice(0));

Notice now the big ball of wibbly-wobbly, timey-wimey stuff has resolved into a simple line from A to B?

This is actually an issue with how the console works. When you log an object, some browsers (particularly Chrome) log a reference to the object so you can browse it freely. However, if the object changes... it doesn't work as expected.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

according to THIS your code works fine.

im using chrome Version 34.0.1847.131 m

Banana
  • 7,424
  • 3
  • 22
  • 43
  • Different consoles I believe – Gga May 01 '14 at 12:18
  • i dont think it makes any difference. javascript is javascript, and despite the different support of the browsers, this array operation is a basic operation. please make sure you didnt reload the array accidentally. it should and will work, and the only thing that comes to mind is accidental re-definition of the array before the 2nd print. – Banana May 01 '14 at 12:21
  • It does. That's why we're seeing different results. See the answer above and similar question – Gga May 01 '14 at 12:25