Gga
Gga

Reputation: 4421

Why does logging an array to console before and after an operation on it show the same output in each case

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.

Upvotes: 3

Views: 45

Answers (2)

Banana
Banana

Reputation: 7483

according to THIS your code works fine.

im using chrome Version 34.0.1847.131 m

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324760

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.

Upvotes: 3

Related Questions