title: Why the return value is different inside & outside the function?
body: <p>I am trying to explore <code>thisArg</code> &amp; <code>filter</code> array method &amp; just experimenting how <code>thisArg</code> can be used &amp; return from <code>filter</code>. I have an array and inside the <code>filter</code> method I am checking if the value exist in <code>thisArg</code> , if not then push the value &amp; finally return <code>thisArg</code> from the array filter function.</p>
  
  <p>Inside the filter function logging <code>thisArg</code> will show <code>[1],[1,2],...</code> but if there is a repeated element it will not be pushed.</p>
  
  <p>On consoling the return from <code>filter</code> the repeated value still exist.</p>
  
  <p>I am not able to understand why the <code>thisArg</code> value differ inside and outside the <code>filter</code> method.</p>
  
  <p><code>thisArg</code> is the optional value.In the below function </p>
  
  <pre><code>array.filter(function(item) {}, []) // this empty array is thisArg
  </code></pre>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var x = [1, 2, 3, 4, 1];
  
  var m = x.filter(function(item) {
    if (this.indexOf(item) === -1) {
      this.push(item)
    }
    // it will console [1],[1,2],[1,2,3]....
    // will never have repeated value
    console.log("Before Final Return ", this);
    return this; // returning this expecting it will not have repeated value
  
  }, [])
  // this will have [1,2,3,4,1]
  console.log("Final Value ", m)</code></pre>
  </div>
  </div>
  </p>