title: JavaScript split() method resulting in undefined
body: <p>I'm working on a simple program that will select a random string from an array.</p>
  
  <p>I started with a predefined array, but about halfway through wondered if it wouldn't be simpler (or more elegant) to use a <code>text file (.txt)</code>, since I have a bit over 1000 items.</p>
  
  <p>I found this solution here (for which I take no credit) and it is working for me...</p>
  
  <pre><code>function readTextFile(file) {
      var items = [];
      var rawFile = new XMLHttpRequest();
      rawFile.open("GET", file, true);
      rawFile.onreadystatechange = function() {
          if(rawFile.readyState === 4)
          {
              if(rawFile.status === 200 || rawFile.status === 0)
              {
                  var items = rawFile.responseText.split('\n');
                  alert(items[0]);
              }
          }
      };
      rawFile.send(null);
  }
  
  readTextFile('source.txt');
  </code></pre>
  
  <p>...to an extent.</p>
  
  <p>I want the <code>array items[]</code> to contain one line per item. In other words I want to split per new line. However, all array items are undefiend after <code>items[0]</code> when I use <code>split('\n')</code>.</p>
  
  <p><code>items[0]</code> in the example above becomes the first sentence, so that much is correct. If I want to <code>alert items[1]</code> I get undefined.</p>
  
  <p>If I use some other split point, such as <code>split('')</code>, it works correctly, separating each character per item ONLY until the line breaks, after which point I get undefined again.</p>
  
  <p>Let's say the first line of the <code>.txt is "asd"</code>:
  so 'asd' is defined in array as :</p>
  
  <pre><code>items[0] = 'a'
  items[1] = 's'
  items[2] = 'd'
  items[3] = undefined
  </code></pre>
  
  <p>This is what I would get. Where am I wrong?</p>
  
  <p>Contents of text file:</p>
  
  <pre><code>asfe
  asdasdasd
  asdasd
  
  fgfg
  </code></pre>