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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:18:33+00:00 2026-06-16T03:18:33+00:00

I have seen anonymous functions inside for loops to induce new scope on the

  • 0

I have seen anonymous functions inside for loops to induce new scope on the web in one or two places and would like to know if it makes sense.

for example:

var attr, colors = ['green','blue','red'];

for ( attr = 0; attr < colors.length; attr++) {
    (function() {
        var colorAttr = colors[attr];

        // do something with colorAttr
    })();
}

I understand it has something to do with keeping the scope inside the for loop clean, but in what situations would this be necessary? Would it be good practice to do this everywhere you need to declare a new var inside the for loop?

  • 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-16T03:18:34+00:00Added an answer on June 16, 2026 at 3:18 am

    2021 Update

    var used to be the only way to declare a variable. But we now have const and let which solve this problem in a better way. These variable declarations do respect the loop as a scope to bind to, which means the following snippet works fine and there is no need for an anonymous function to capture those values.

    const colors = ['green', 'blue', 'red'];
    
    for (let i = 0; i < colors.length; i++) {
        const color = colors[i];
        setTimeout(function() {
            alert(color);
        }, i * 1000);
    }
    

    What follows below is my original answer to this question from 2012.


    When you have inner functions that are not executed immediately, as part of the loop.

    var i, colors = ['green', 'blue', 'red'];
    
    for (i = 0; i < colors.length; i++) {
        var color = colors[i];
        setTimeout(function() {
            alert(color);
        }, i * 1000);
    }
    
    // red
    // red
    // red
    

    Even though var color is inside the loop, loops have no scope. You actually only have one variable that every loop iteration uses. So when the timeouts fire, they all use the same value, the last value set by the loop.

    var i, colors = ['green', 'blue', 'red'];
    
    for (i = 0; i < colors.length; i++) {
        (function(color) {
            setTimeout(function() {
                alert(color);
            }, i * 1000);
        })(colors[i]);
    }
    
    // green
    // blue
    // red
    

    This one captures the value at each iteration into an argument to a function, which does create a scope. Now each function gets it’s own version of a color variable which won’t change when functions created within that loop are later executed.

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

Sidebar

Related Questions

I have seen in a few libraries, Spray for example, dual package specifications like
I am new to DBMS_PROFILER. All the examples I have seen use a simple
I have seen JavaScript written like this (it was at a demonstration, and I
I have a project i am working on currently and would like to implement
I have seen general posts on how to track anonymous users in a voting
I have seen several postings for issues with consuming AJAX web services and the
For a startup website. Should have this - 1.) What they would like to
Have seen some similar questions: What is the difference between a JavaBean and a
I have seen some people in SO commenting that Singleton Pattern is an anti-pattern.
I have seen behaviour in Perforce that I am unable to explain. I took

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.