JavaScript console prints assigned value of variable before it has been assigned?
<p>I'm deeply confused by the behaviour of either JavaScript, or the Chrome console. Can someone help me understand?</p>

<p>Basically I have the following JavaScript code, not nested inside any function or other scope: </p>

<pre><code>var initial_array = [];

function initialiseArray() { 
   initial_array = [2, 9, 8, 6, 0, 2, 1];
} 

function copyToNewArray() {
    var copied_array = [];

    console.log("COPIED 1", copied_array);

    for (var i = 0; i &lt; initial_array.length; i++) {
        var copy = initial_array[i];
        copied_array.push(copy);
    }

    console.log("COPIED 2", copied_array);
}

initialiseArray();
copyToNewArray();
</code></pre>

<p>I would expect <code>COPIED 1</code> to print <code>[]</code> - as the variable hasn't been assigned yet - but instead it prints <code>[2, 9, 8, 6, 0, 2, 1]</code> - ie the value after it has been assigned.</p>

<p>Why? </p>

<p>Incidentally, if you replace lines 8-11 with <code>initial_array = copied_array</code>, then <code>RESULTS 1</code> does indeed print as <code>[]</code>. Is it something to do with using <code>.push</code>?</p>
