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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:25:01+00:00 2026-06-18T05:25:01+00:00

Possible Duplicate: Javascript closure inside loops – simple practical example I’m playing around with

  • 0

Possible Duplicate:
Javascript closure inside loops – simple practical example

I’m playing around with setTimeout in a project of mine in order to throttle the adding of elements to the DOM (so UI won’t freeze during page loading). However, I’ve encountered something a bit puzzling to me. Given this code:

for(var i = 0; i < 5; i++) {
    var j = i + 10;
    console.log("i is: " + i + " j is: " + j);
    setTimeout(function() {
        console.log("in timeout i is: " + i + " j is: " + j);
    }, i * 1000);
}

I get the following output:

i is: 0 j is: 10
i is: 1 j is: 11
i is: 2 j is: 12
i is: 3 j is: 13
i is: 4 j is: 14
in timeout i is: 5 j is: 14
in timeout i is: 5 j is: 14
in timeout i is: 5 j is: 14
in timeout i is: 5 j is: 14
in timeout i is: 5 j is: 14

That the value of i in the timeout is 5 is obvious since i is scoped in the for loop initialization. However, how come j is 14 for all timeout outputs? I would have thought that j would have output 10, 11, 12, 13, 14 in the timeout since it is scoped within the loop. How could I achieve that result?

  • 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-18T05:25:02+00:00Added an answer on June 18, 2026 at 5:25 am

    That is because, in JavaScript, var has function scope.

    var declarations will be hoisted up to the top of the current execution context. That is, if it is inside of a function, the var will be the scoped inside the function’s execution context, otherwise the program (global) execution context.

    ECMAScript 2015 (a.k.a. ES6) introduces let which lets you create block scope vars, but as it is not widely supported I’ll just leave the link for reference.

    An workaround, to still use var and have it “scoped” inside the loop, is to create a new execution context, also know as closure:

    function callbackFactory(i, j) {
        // Now `i` and `j` are scoped inside each `callbackFactory` execution context.
        return function() { // This returned function will be used by the `setTimeout`.
           // Lexical scope (scope chain) will seek up the closest `i` and `j` in parent
           // scopes, that being of the `callbackFactory`'s scope in which this returned
           // function has been initialized.
           console.log("in timeout i is: " + i + " j is: " + j);
        };
    }
    for(var i = 0; i < 5; i++) {
        var j = i + 10;
        console.log("i is: " + i + " j is: " + j);
        setTimeout( callbackFactory(i, j), i * 1000);
    }
    

    As I scoped both i and j inside the callback scope, they will return the same values inside the setTimeout than they had when they were passed to callbackFactory.

    See Live demo.

    Another way to do the same thing is to create an IIFE inside the for loop. This is usually simpler to read but JS(H|L)int will yell at you. ;) This is because creating functions inside a loop is considered bad for performance.

    for(var i = 0; i < 5; i++) {
        var j = i + 10;
        console.log("i is: " + i + " j is: " + j);
        (function(i, j) { // new execution context created for each iteration
            setTimeout(function() {
                console.log("in timeout i is: " + i + " j is: " + j);
            }, i * 1000);
        }(i, j)); // the variables inside the `for` are passed to the IIFE
    }
    

    Above I’ve created a new execution context inside the for in each iteration. (Demo)

    Mixing the first approach (callbackFactory) with the IIFE above, we could even make a 3rd option:

    for(var i = 0; i < 5; i++) {
        var j = i + 10;
        console.log("i is: " + i + " j is: " + j);
        setTimeout(function(i, j) {
            return function() {
                console.log("in timeout i is: " + i + " j is: " + j);
            };
        }(i, j), i * 1000);
    }
    

    This is simply using an IIFE in the place of the callbackFactory function. This doesn’t seem very easy to read and still creates functions inside the for loop which is bad for performance, but just noting that this is also possible and works.

    These 3 approaches are very commonly seen in the wild. =]


    Oh, almost forgot to answer the main question. Just put the callbackFactory in the same scope as the for loop, then instead of scoping the i inside of it, let the scope chain seek the i of the outer scope:

    (function() {
        var i, j;
        function callbackFactory(j) {
        // the `j` inside this execution context enters it as a formal parameter,
        // shadowing the outer `j`. That is, it is independent from the outer `j`.
        // You could name the parameter as "k" and use "k" when logging, for example.
            return function() {
               // Scope chain will seek the closest `j` in parent scopes, that being
               // the one from the callbackFactory's scope in which this returned
               // function has been initialized.
               // It will also seek up the "closest" `i`,
               // which is scoped inside the outer wrapper IIFE.
               console.log("in timeout i is: " + i + " j is: " + j);
            };
        }
        for(i = 0; i < 5; i++) {
            j = i + 10;
            console.log("i is: " + i + " j is: " + j);
            setTimeout( callbackFactory(j), i * 1000);
        }
    }());
    /* Yields:
    i is: 0 j is: 10  
    i is: 1 j is: 11  
    i is: 2 j is: 12  
    i is: 3 j is: 13  
    i is: 4 j is: 14  
    in timeout i is: 5 j is: 10  
    in timeout i is: 5 j is: 11  
    in timeout i is: 5 j is: 12  
    in timeout i is: 5 j is: 13  
    in timeout i is: 5 j is: 14 */
    

    Fiddle

    Note that I’ve moved the i and j declarations to the top of the scope solely for readability. It has the same effect as for (var i = [...], which would be hoisted up by the interpreter.

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

Sidebar

Related Questions

Possible Duplicate: Javascript closure inside loops - simple practical example Calling setTimeout function within
Possible Duplicate: Javascript closure inside loops - simple practical example I'm having a problem
Possible Duplicate: Javascript closure inside loops - simple practical example Recently I have been
Possible Duplicate: Javascript closure inside loops - simple practical example I am trying to
Possible Duplicate: Javascript closure inside loops - simple practical example Please explain the use
Possible Duplicate: Javascript closure inside loops - simple practical example javascript variable scope/closure in
Possible Duplicate: Javascript closure inside loops - simple practical example I have an array
Possible Duplicate: Javascript closure inside loops - simple practical example I was trying to
Possible Duplicate: Javascript closure inside loops - simple practical example I add event handlers
Possible Duplicate: Javascript closure inside loops - simple practical example I'm creating a table

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.