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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:10:11+00:00 2026-06-06T09:10:11+00:00

I am trying to undrstand the code for(var i = 0; i < 10;

  • 0

I am trying to undrstand the code

for(var i = 0; i < 10; i++) {
    setTimeout((function(e) {
        return function() {
            console.log(e);
        }
    })(i), 1000)
}

from here http://bonsaiden.github.com/JavaScript-Garden/#function.closures

I understood this method :

for(var i = 0; i < 10; i++) {
    (function(e) {
        setTimeout(function() {
            console.log(e);  
        }, 1000);
    })(i);
}

Can anyone please help me by explaining the first one?

I will try to explain how I understands the first one,

first i is 0,
setTimeout is called,
self calling function "function(e)" is called with i=0,
Im stuck!! what happens when this function returns a function?
  • 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-06T09:10:14+00:00Added an answer on June 6, 2026 at 9:10 am

    “Can you please check the updated question specifying where im getting confused”

    OK, here’s the long explanation. Remember that the first parameter to setTimeout() needs to be a reference to the function that you want executed after the specified delay. The simplest case is to just name a function defined elsewhere:

    function someFunc() {
       console.log("In someFunc");
    }
    
    setTimeout(someFunc, 100);
    

    Note there are no parentheses on someFunc when passing it as a parameter to setTimeout because a reference to the function itself is required. Contrast with:

    setTimeout(someFunc(), 100);   // won't work for someFunc() as defined above
    

    With parenthese it calls someFunc() and passes its return value to setTimeout. But my definition of someFunc() above doesn’t explictly return a value, so it implicitly returns undefined – which is like saying setTimeout(undefined, 100).

    But it would work if changed someFunc() to return a function instead of returning undefined:

    function someFunc() {
       return function() {
          console.log("In the function returned from someFunc");
       };
    }
    

    So now (at last) we come to the code from your question:

    setTimeout((function(e) {
        return function() {
            console.log(e);
        }
    })(i), 1000)
    

    Instead of referencing a function by name and calling it as someFunc(i) it defines an anonymous function and calls it immediately as (function(e) {})(i). That anonymous function returns another function and it is that returned function that becomes the actual parameter to setTimeout(). When the time is up it is that returned function that will be executed. Because the (inner) function being returned is defined in the scope of the (outer) anonymous function it has access to the e parameter.

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

Sidebar

Related Questions

I’m trying to understand code from http://www.yesodweb.com/book/conduits . After some fixes (like replacing Resource
I am trying to understand how these jquery code work function findVideoForElement(el) { var
I have this code: var originalBorder = container.css("border"); container.hover(function(event) { event.stopPropagation(); $(this).css("border", "1px solid
I found the following code example (from Telerik ) that I'm trying to understand.
I am trying to develop a Firefox extension. The following code: var divList =
This is a function taken from Pro JavaScript techniques that I'm trying to understand,
Here is a little Javascript code snippet where I have used a function to
I'm trying to understand how this piece of code from Keith Peter's Advanced Actionscript
I'm a little stuck with trying to set a setTimeout to a function call
i'm trying to encapsolate my code inside an immediate function that later on will

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.