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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:46:02+00:00 2026-05-25T15:46:02+00:00

I had a bug, it went like this for(var i=0; i<arr.length; i++){ var v

  • 0

I had a bug, it went like this

for(var i=0; i<arr.length; i++){
    var v = arr[i]
    var obj=new Thing
    obj.TheCallback = function(e) { blah = v; domorestuff(); ... }
    obj.runCodeToExecuteTheCallback()
}

The problem is v inside the function is using the v from the last loop. It’s a closure thing where you can access variables in your parent scope. But my question is…

Why is javascript reusing v in each iteration? When it goes out of scope (at the end of the loop) i dont except it to be MODIFIED from any other scope unless it was passed in (such as the inner function). Why the heck is javascript clobbering my variable? Is there some kind of reason for this design? Is it a bug that has been decided never to be fixed?

  • 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-25T15:46:03+00:00Added an answer on May 25, 2026 at 3:46 pm

    This is a very common issue people encounter.

    JavaScript doesn’t have block scope, just function scope. So each function you create in the loop is created in the same variable scope, so they’re referencing the same v variable.

    To create a new scope, you need to invoke a function, and pass in whatever you want to exist in a new scope.

    function createCallback( x ) {
        return function(e) { blah = x; domorestuff(); ... }
    }
    
    for(var i=0; i<arr.length; i++){
        var v = arr[v]
        var obj=new Thing
        obj.TheCallback = createCallback( v );
        obj.runCodeToExecuteTheCallback()
    }
    

    Here I invoked the createCallback() function, pass in v, and had createCallback() return a function that references the local variable (named x in the function, though you could name it v as well).

    The returned function is of course assigned to obj.TheCallback.

    Its variable scope exists as long as the function exists, so it will continue to have reference to any variables that it needs that were in the variable scope when it was created.

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

Sidebar

Related Questions

I had a bug in my code that went like this. char desc[25]; char
I had this nasty bug that disappeared in the past but now after quite
I thought I had seen a bug report about this on the jQuery site,
I had this question earlier and it was concluded it was a bug in
I've had this bug pop up before with Firefox wherein my .swf files that
I had an bug cuz of this, and it made me wonder why it
I've had a bug that has been bugging me for days. I'm pretty new
I've had a bug in our software that occurs when I receive a connection
Did you ever had a bug in your code, you could not resolve? I
I have had a bug recently that only manifested itself when the library was

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.