Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6536363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:29:03+00:00 2026-05-25T10:29:03+00:00

I got a piece of code for javascript which I just do not understand:

  • 0

I got a piece of code for javascript which I just do not understand:

function dmy(d) {
    function pad2(n) {
        return (n < 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;
}

How can a function be defined within another function? Can we call pad2() from outside of my() function?

Please put some light on it. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:29:04+00:00Added an answer on May 25, 2026 at 10:29 am

    Functions are another type of variable in JavaScript (with some nuances of course). Creating a function within another function changes the scope of the function in the same way it would change the scope of a variable. This is especially important for use with closures to reduce total global namespace pollution.

    The functions defined within another function won’t be accessible outside the function unless they have been attached to an object that is accessible outside the function:

    function foo(doBar)
    {
      function bar()
      {
        console.log( 'bar' );
      }
    
      function baz()
      {
        console.log( 'baz' );
      }
    
      window.baz = baz;
      if ( doBar ) bar();
    }
    

    In this example, the baz function will be available for use after the foo function has been run, as it’s overridden window.baz. The bar function will not be available to any context other than scopes contained within the foo function.

    as a different example:

    function Fizz(qux)
    {
      this.buzz = function(){
        console.log( qux );
      };
    }
    

    The Fizz function is designed as a constructor so that, when run, it assigns a buzz function to the newly created object. That is, you’d use it like this:

    const obj = new Fizz();
    obj.buzz();
    

    or more concisely (if you don’t need to keep the object after calling buzz):

    new Fizz().buzz();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a JSP page with a piece of Javascript validation code which limits
I've got a piece of code which contains a for loop to draw things
I've got this piece of code: <?php $userid = $me['id']; function showifuserexists() { ?>
I've got a small piece of code that is parsing an index value to
I've got the following piece of code in an aspx webpage: Response.Redirect(/Someurl/); I also
I've got a LOT of tests written for a piece of software (which is
I have got a piece of code, that should countdown some number (in this
I've got a piece of JavaScript that inserts/removes an iframe from the DOM. Everything
i got this piece of code: - (void)postToWall { FBStreamDialog *dialog = [[FBStreamDialog alloc]
I came across this piece of code and completely got lost interpreting its meaning.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.