title: all possible combinations in javascript - why pop()?
body: <p>I'm trying to understand this function that I came across online that returns all possible combinations of a string -- why exactly is the pop() call needed with nextLetter.pop? I tried debugging this in the console but it's not clear what the pop does exactly.</p>
  
  <p>result with nextLetter.pop() ---> [ 'bac', 'bca', 'cba', 'cab', 'acb', 'abc' ]</p>
  
  <p>result without nextLetter.pop() --> [ 'bac',
    'bacca',
    'baccacba',
    'baccacbaab',
    'baccacbaabacb',
    'baccacbaabacbbc' ]</p>
  
  <pre><code>function stringPermutations(str) {
    var permutations = [];
    var nextLetter = [];
    var chars = str.split('');
  
    permutateInner(chars);
  
  
  
    function permutateInner(chars) {
      if (chars.length === 0) {
  
        permutations.push(nextLetter.join(''));
      }
      for (var i = 0; i &lt; chars.length; i++) {
  
  
        chars.push(chars.shift());
  
        nextLetter.push(chars[0]);
  
        permutateInner(chars.slice(1));
        //what is this doing?
  
        nextLetter.pop();
  
      }
    }
    return permutations;
  }
  
  console.log(stringPermutations('abc'));
  </code></pre>