title: return value of a callback function in Node.js
body: <p>I have a question about callback functions. Consider this simple code:</p>
  
  <pre><code>var array = [1,2,3];
  
     console.log(array.map(function(val){
  
     return val * 2
  
  }))
  </code></pre>
  
  <p>This will correctly log back to the console <code>[2,4,6]</code> which means it is "returning" that value back.</p>
  
  <p>Now consider this (<strong>NOTE:</strong> assume "ab" to be a valid directory)</p>
  
  <pre><code>fs.readdir("ab",function(err,data){
     console.log(data)
  })
  </code></pre>
  
  <p>This code works fine and will print an array of filenames in the directory "ab" to the terminal. However, the same code:</p>
  
  <pre><code>console.log(fs.readdir("ab",function(err,data){
      return data
  }))
  </code></pre>
  
  <p>This will print <code>undefined</code>.. why is this? if in the previous snippet I can log <code>data</code> to the terminal, why can't I return the value and log it to the console? And how is the first snippet including the <code>map</code> method working using this same logic?</p>
  
  <p>Thank you.</p>