javascript arrays in and outside of functions
<p>I have some javascript array that I append within a function and call after the function has run (the db information is a call to CouchDB):</p>

<pre><code>$(document).ready(function() {

  my_array = [];

  view_name = db_name+'/farms_by_name'
  $db.view(view_name, {
    success: function(data) {
       console.log(data)
       for (i in data.rows) {
           console.log(data.rows[i]);
           plot_name.push(data.rows[i].value.name);
           my_array.push("0");
           console.log(my_array)
           console.log(my_array.length)
        }
    },
    error: function(e) {
      alert('Error loading from database: ' + e);
    }
  });

  console.log(my_array);
  console.log(my_array.length);

});
</code></pre>

<p>Using firebug, I see that within the function the array is appended correctly, for e.g. a single element the console.log returns</p>

<pre><code>my_array = ["0"]
my_array.length = 1
</code></pre>

<p>but the console.log after the function (which appears first in the firebug log) shows:</p>

<pre><code>my_array = [ ]
my_array.length = 1
</code></pre>

<p>and my_array when inspected in the log reveals:</p>

<pre><code>[ ]
0              "0"
</code></pre>

<p>Can anyone explain this behaviour to me and explain how I should correctly append the array such that I can call the length and loop through it correctly outside of the db success function?</p>

<p>Thanks!</p>
