title: How does this.head gets directly updated when not updated?
body: <p>My LinkedList Class has two properties</p>
  <ol>
  <li>this.head</li>
  <li>this.size</li>
  </ol>
  <p><code>this.head</code> is used to contain the list of objects. Simple logic says, if I update <code>this.head</code> , its value should be updated. If I, however, assign its value to some other variable and then try to update that variable, then the original this.head shouldn't update.</p>
  <p>Eg:</p>
  <pre><code>printElements(){
      let current = this.head;
      while(current){
        console.log(current.data)
        current = current.next;
      }
    };
  </code></pre>
  <p>But this piece of code contradicts that situation. I don't know why <code>this.head</code> is getting updated here when it's not directly referenced.</p>
  <pre><code>insertAt(data, index){
  
      if(index &gt; 0 &amp;&amp; index &gt; this.size)
        return 
      
      const node = new Node(data);
      let current, previous;
  
      current = this.head;
      let count = 0;
  
      while(count &lt; index){
        previous = current;
        count++;
        current = current.next;
      }
  
      node.next = current;
      previous.next = node;
  
    };
  </code></pre>
  <p>The entire piece of code</p>
  <pre><code>class Node{
    constructor(data, next = null){
      this.data = data;
      this.next = next;
    }
  }
  
  class LinkedList{
    constructor(){
      this.head = null;
      this.size = 0;
    };
      
    insert(data){
      this.head = new Node(data, this.head);
      this.size++;
    };
  
    insertAt(data, index){
  
      if(index &gt; 0 &amp;&amp; index &gt; this.size)
        return    
  
      const node = new Node(data);
      let current, previous;
  
      current = this.head;
      let count = 0;
  
      while(count &lt; index){
        previous = current;
        count++;
        current = current.next;
      }
  
      node.next = current;
      previous.next = node;
  
    };
  
    printElements(){
      let current = this.head;
      while(current){
        console.log(current.data)
        current = current.next;
      }
    };
  
  }
  </code></pre>