title: Need help understanding recursive function to generate all combinations of a string
body: <p>I came across <a href="https://codereview.stackexchange.com/questions/7001/generating-all-combinations-of-an-array">this code</a>, but am having a hard time figuring out how the flow of this function is working. </p>
  
  <pre><code>function combinations(str) {
      var fn = function(active, rest, a) {
          if (!active &amp;&amp; !rest)
              return;
          if (!rest) {
              a.push(active);
          } else {
              fn(active + rest[0], rest.slice(1), a);
              fn(active, rest.slice(1), a);
          }
          return a;
      }
      return fn("", str, []);
  }
  </code></pre>
  
  <p>I've console.logged a bunch of statements, but I'm getting lost in the recursion. Specifically, I don't understand what's happening after the first of the two <code>fn</code>'s gets returned. It's simply returning <code>a</code>, but to me, it appears as if the second <code>fn</code> knows to take the returned <code>a</code>. How does that work? Shouldn't it need a variable assignment like so:</p>
  
  <pre><code>a = fn(active + rest[0], rest.slice(1), a);
  fn(active, rest.slice(1), a);
  </code></pre>