title: JavaScript: Array is undefined
body: <p>I have a multi-line variable and want to select a random line:</p>
  
  <pre><code>$.get('namelist.txt', function(nameList) {
    name = nameList.split('\n');
    var i = random();
    alert(name[i]); // this is undefined
  });
  </code></pre>
  
  <p>The <code>random()</code> function returns a random number:</p>
  
  <pre><code>function random() {
    return Math.floor(Math.random()*201);
  }
  </code></pre>
  
  <p>The problem is that the alert says <code>undefined</code>. If I replace line 3 with <code>var i = 5</code>, it works. I tested <code>i</code> with <code>typeof</code> and they're numbers in both cases. Any ideas on how to fix this?</p>