title: How would you write the 'context' variable while creating the ._each function (underscore.js)
body: <p>I'm trying to recreate the _.each() func from underscore.js, however cannot get the 'context' variable correct. I understand what it's meant to do, but seem to going in circles trying to implement it. I've marked the area of the code below with comments. Any tips appreciated, thanks!</p>
  <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>_.each = function (collection, iteratee, context) {
    //-----context--------
    if (context) {
      if (Array.isArray(collection)) {
        for (let i = 0; i &lt; collection.length; i++) {
          iteratee.call(context, collection[i], collection);
        }
      } else {
      for (const key in collection) {
        iteratee.call(context, collection[key], collection);
      }
    }
      return collection;
    }
    //-----end of context----
    //check if array or object
    if (Array.isArray(collection)) {
      for (let i = 0; i &lt; collection.length; i++) {
        iteratee(collection[i], i, collection);
      }
    } else {
      for (const key in collection) {
        iteratee(collection[key], key, collection);
      }
    }
    return collection;
  };</code></pre>
  </div>
  </div>
  </p>