title: javascript method array.shift() returning undefined
body: <p>I have this piece of code where I delete the first (or last) element from an array, according to a boolean variable. I do like this:</p>
  
  <pre><code>{...
  console.log("length before " + waypoints.length)
  waypoints = this.deleteWaypoint(waypoints)
  console.log("length after " + waypoints.length)
  ...}
  
  deleteWaypoint(waypoints){
  if (this.first){
    this.first = false;
    return waypoints.shift() 
  } else {
    this.first = true;
    return waypoints.pop() 
  }
  }
  </code></pre>
  
  <p>The first log prints that <code>waypoints</code> has a certain length, then I call the method to delete elements and when the second log prints is <code>length after undefined</code>.  "First" is a global variable initialized to true. 
  Why is that? </p>