title: Does the return statement work from inner to outter?
body: <p>I have been struggling with the return statement in javascript.  Here is <a href="http://msdn.microsoft.com/en-us/library/22a685h9%28v=vs.94%29.aspx" rel="nofollow">msdn definition</a></p>
  
  <p>Here is a function I wrote that converts a binary number to a decimal number:</p>
  
  <pre><code>function bin2dec(num){
      **return** num.toString().split('').reverse().reduce(function(preVal, currentVal , iterator){
          **return** (currentVal === '1') ? preVal + Math.pow(2, iterator) : preVal;
      }, 0);
    }
  </code></pre>
  
  <p>Let's use the 5 in binary as an example to work with Binary5 = 101</p>
  
  <p>So I thought whenever you use a return statement, it exits the current function prematurely. </p>
  
  <p>Here I have two return statements, one within a callback function.</p>
  
  <p>The first return statement is on the second line:</p>
  
  <pre><code>return num.toString().split('').reverse() // and then the reduce function w/ a call back.
  </code></pre>
  
  <p>I believe this 'return' statement is waiting for the reduce function's return </p>
  
  <p>The function takes the number, makes it a string, then splits, then reverses, then the reduce method iteates over the array. When the reduce function is complete it returns a value which is then also returned by the first return value. Here is more detail about the reduce functions return value.</p>
  
  <p>In the callback:</p>
  
  <pre><code>// 101
      .reduce(function(preVal, currentVal , iterator){
              return (currentVal === '1') ? preVal + Math.pow(2, iterator) : preVal;
          }, 0);
  </code></pre>
  
  <p>It iterates over each part of the array, in this case the 'return' is preVal unless the
  currentVal === 1.  The 'return' value is passed to the callback and is the value of preVal. for each loop we are adding and building on the current val from the pre val. When every item in the array is iterated on, the return statement is the final value. That final value is then returned from the 1st return statement that did not execute yet.</p>
  
  <p>I really get lost with return statements at times.  I am mainly trying to check if my logic in how the 'return' statements are acting the way I am describing. For a quick recap</p>
  
  <ol>
  <li><p>First Return - waits for everything to be evaluated so there is an
  object to return.</p></li>
  <li><p>Second Return - is used for the callback and when it is done
      iterating, the last return value is returned, then that value is
      acted on from the first return statement that ultimately breaks out
      of the function.</p></li>
  </ol>