title: Linked List Data Structure with Javascript
body: <p>I'm trying to figure out the linked list data structure using Javascript. But there is a part that I'm not able to understand.</p>
  
  <pre><code>function LinkedList() {
    var Node = function(element) {
      this.element = element;
      this.next = null;
    }
  
    var length = 0;
    var head = null;
  
    this.append = function(element) {
      var node = new Node(element),
        current;
  
      if (head === null) {
        head = node;
      } else {
        current = head;
  
        //loop the list until find last item
        while (current.next) {
          current = current.next
        }
  
        //get last item and assign next to node to make the link
        current.next = node
      }
      length++;
    }
  
    this.removeAt = function(position) {
      //check for out of bounds values
      if (position &gt; -1 &amp;&amp; position &lt; length) {
        var current = head,
          previous,
          index = 0;
  
        if (position === 0) {
          head = current.next;
        } else {
          while (index++ &lt; position) {
            debugger;
            previous = current;
            current = current.next;
          }
  
          previous.next = current.next;
        }
  
        length--;
  
        return current.element;
  
      } else {
        return null;
      }
    }
  
    this.toString = function() {
      var current = head,
        string = '';
  
      while (current) {
        string = current.element;
        current = current.next;
      }
  
      return string;
    }
  }
  
  var list = new LinkedList();
  list.append(15);
  list.append(10);
  list.append(11);
  list.removeAt(1);
  </code></pre>
  
  <p>I don't  understand how the variable <strong><em>current</em></strong> loses its reference to the node when you run the <em>removeAt</em> method.</p>