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 7777761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:13:31+00:00 2026-06-01T18:13:31+00:00

Is there a definite source on variable capture in Javascript besides the standard (it’s

  • 0

Is there a definite source on variable capture in Javascript besides the standard (it’s a pain to read the standard)?

In the following code i is copied by value:

for (var i = 0; i < 10; i++)
{
    (function (i)
    {
        process.nextTick(function ()
        {
            console.log(i)
        })
    }) (i)
}

So it prints 1..10. process.nextTick is an analog of setTimeout(f,0) in node.

But in the next code i doesn’t seem to be copied:

for (var i = 0; i < 10; i++)
{
        var j = i
        process.nextTick(function ()
        {
            console.log(j)
        })
}

It prints 9 10 times. Why? I’m more interested in a reference/general article than in explaining this concrete case of capture.

  • 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-01T18:13:33+00:00Added an answer on June 1, 2026 at 6:13 pm

    I don’t have a handy reference. But the bottom line is: In the first, you’re explicitly passing in i to an anonymous function, which creates a new scope. You are not creating a new scope for either i or j in the second. Also, JavaScript always captures variables, not values. So you would be able to modify i too.

    The JavaScript var keyword has function scope, not block scope. So a for loop does not create a scope.

    As a note, the non-standard let keyword has local scope.

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

Sidebar

Related Questions

I see there is a question here but there is no definite answer. Has
Okay, I browsed through a few topics here, but there was no definite answer.
Is there any definite guide how to make QWidget app portrait oriented on MeeGo
I have a slightly vague question. I have the following in my code: http://jsfiddle.net/PMnmw/2/
do the entries in the XCode profiler Leaked Blocks table imply there was definite
Is there any way, purely in MSSQL, to determine if the following maxtrix would
I have a standard code to call a .net webservice I get exception org.xmlpull.v1.xmlpullparserexception
Is there a standard way of using the jquery-ui icon sprite without attaching them
I suspect the answer's a pretty definite no, but given the following html and
Is there a definite DO and DONT when implementing seo urls? A lot of

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.