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

  • Home
  • SEARCH
  • 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 7756529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:50:50+00:00 2026-06-01T12:50:50+00:00

I have a question when I do the node.js async coding. Here is the

  • 0

I have a question when I do the node.js async coding.
Here is the example codes:

function foo(arg) {
  var a = arg;
  console.log(a + ' start');
  setTimeout(function () {console.log(a);}, 500);
};
foo(1);
foo(2);

It outputs:

1 start
2 start
1
2

I’m confused. I thought it should output↓, because the local variable is changed by the foo(2)

1 start
2 start
2
2

Could you guys please tell me why/how node.js keep the local variable for the internal callback function access?
Thanks a lot!

  • 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-06-01T12:50:51+00:00Added an answer on June 1, 2026 at 12:50 pm

    Because it is a local variable, not a global. That’s the point of local variables.

    var a creates a variable that exists for the life time of the function call. Declaring a function inside it extends the lifetime to cover that function too. When the anonymous function is called it continues to use the a that exists in the scope that it was created in.

    Since you call the outer function twice, you have two as. One for each call. You have two anonymous functions, one for each call. Each anonymous function was created in the same scope as one of the as.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have question regarding the use of function parameters. In the past I have
I have question concerning a function I created. I would like to show the
I have a question about how would I remove a child from a node
I have a question related to Key Type initialization in HashMap. For example, I
Have a question about typedef in C. I have defined struct: typedef struct Node
I start to look at Node.js. Also I'm using Express. And I have a
I have a question about the following code private void printTree(Node node){ if(node==null) return;
I have a question about using databases... string node = Session[node].ToString(); SqlConnection dataConnection =
I am working on a CMS system for Node.js, I have a quick question
i have a question about this code. class Queue { Node first, last void

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.