title: JavaScript Nested function
body: <p>I got a piece of code for javascript which I just do not understand:</p>
  
  <pre><code>function dmy(d) {
      function pad2(n) {
          return (n &lt; 10) ? '0' + n : n;
      }
  
      return pad2(d.getUTCDate()) + '/' +
         pad2(d.getUTCMonth() + 1) + '/' +
         d.getUTCFullYear();
  }
  
  function outerFunc(base) {
      var punc = "!";
  
      //inner function
      function returnString(ext) {
         return base + ext + punc;
      }
  
      return returnString;
  }
  </code></pre>
  
  <p>How can a function be defined within another function? Can we call pad2() from outside of my() function? </p>
  
  <p>Please put some light on it. Thanks</p>