title: .trim() is not a function
body: <p>I have a very simple string that can contains a list which can possibly contain whitespace:</p>
  
  <pre><code>string = "one, two,three ";
  </code></pre>
  
  <p>I want to first split the string by <code>,</code> to create an array of three strings and then remove any whitespace using <code>.trim()</code></p>
  
  <pre><code>array = string.split(',').trim();
  </code></pre>
  
  <p>which returns <code>"one","two","three"</code></p>
  
  <p>however sometimes it fails and returns an error <code>.trim() is not a function</code> </p>
  
  <p>I read that <code>.trim()</code> returns a new string not a trimmed version of the current string. So i used a for loop to do the above:</p>
  
  <pre><code>array = string.split(',');
  for (var i = 0; i &lt; array.length; i++) {
      var item = array[i].trim();
      array.push(item);
  }
  </code></pre>
  
  <p>which returns <code>"one","two","three"</code></p>
  
  <p>my question is, can anyone explain why i was getting the error only sometimes? if the array never changed from my example and can anyone provide a cleaner solution to my fix. </p>