title: What use does the JavaScript forEach method have (that map can't do)?
body: <p>The only difference I see in map and foreach is that <code>map</code> is returning an array and <code>forEach</code> is not. However, I don't even understand the last line of the <code>forEach</code> method &quot;<code>func.call(scope, this[i], i, this);</code>&quot;. For example, isn't &quot;<code>this</code>&quot; and &quot;<code>scope</code>&quot; referring to same object and isn't <code>this[i]</code> and <code>i</code> referring to the current value in the loop?</p>
  <p>I noticed on another post someone said &quot;Use <code>forEach</code> when you want to do something on the basis of each element of the list. You might be adding things to the page, for example. Essentially, it's great for when you want &quot;side effects&quot;. I don't know what is meant by side effects.</p>
  <pre><code>Array.prototype.map = function(fnc) {
      var a = new Array(this.length);
      for (var i = 0; i &lt; this.length; i++) {
          a[i] = fnc(this[i]);
      }
      return a;
  }
  
  Array.prototype.forEach = function(func, scope) {
      scope = scope || this;
      for (var i = 0, l = this.length; i &lt; l; i++) {
          func.call(scope, this[i], i, this);
      }
  }
  </code></pre>
  <p>Finally, are there any real uses for these methods in JavaScript (since we aren't updating a database) other than to manipulate numbers like the following?</p>
  <pre><code>alert([1,2,3,4].map(function(x){ return x + 1})); // This is the only example I ever see of map in JavaScript.
  </code></pre>