title: Highly confused about how this simple recursive function works - how to explain it?
body: <p>I had to create function to return a number raised to an exponent. I solved it with a loop:</p>
  
  <pre><code>function pow(x, n) {
    let result = 1;
  
    // multiply result by x n times in the loop
    for (let i = 0; i &lt; n; i++) {
      result *= x;
    }
  
    return result;
  }
  </code></pre>
  
  <p>I saw another function that uses recursion for this. I'm aware that recursion can involve a function calling itself, but in this case, how does the result end up getting added onto the "stack" and return the right answer? No matter how I try to think about it I can't understand how this tiny function works.</p>
  
  <p>Maybe a simple analogy would help - any thoughts? It's not making sense when I try to run through the function in my head :/</p>
  
  <pre><code>function pow(x, n) {
      if (n == 1) {
        return x;
      } else {
        return x * pow(x, n - 1);
      }
  }
  
  alert( pow(2, 3) ); // 8
  </code></pre>