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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:13:05+00:00 2026-06-10T01:13:05+00:00

What is the difference in the use of the below methods? First Method :

  • 0

What is the difference in the use of the below methods?

First Method :

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

for(var i = 0; i < 10; i++) {
    createTimeoutFunction(i);
}

Second Method :

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

for(var i = 0; i < 10; i++) {
    createTimeoutFunction(i);
}

I am new to node js and in using closures. Though both methods return the same output but the second method runs with error. I am not understanding why do we need to use two loops as in the first method. Can’t we just execute as like the second method?

  • 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-10T01:13:06+00:00Added an answer on June 10, 2026 at 1:13 am

    A closure in javascript works the same regardless of the environment or interpreter that you’re using.

    A closure provides a specific variable scope in which that set of code executes. Your closure will have access (obviously) to anything defined within itself, as well as any objects defined in any enclosing closure.

    The problem with you code up there is that in the first example createTimeoutFunction isn’t defined anywhere — when you call it it will fail. In that example:

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

    The function being defined within this closure is an anonymous function. It has no name property and can’t be referenced.

    The second version defines a global variable createTimeoutFunction which is then accessible in ANY closure on the page since it’s part of the global object. (Well I’m assuming that since you’re not showing that this code is enclosed by anything else).

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

Sidebar

Related Questions

In my function, I first use mod as below: void function(int, int, unsigned int);
What is the difference to use {text-decoration: underline} and {border-bottom: ...} ? which is
I'm doing some tests, but I see no difference when I use or not
What difference does it make when I use a const parameter in a procedure?
How can I use DATEDIFF to return the difference between two dates in years,
I don't know the difference beetween await a task and use task.Wait() but for
What is the difference between them? If I use them both on my local
How would I use nHibernate,configured by fluent nhibernate if it makes any difference, to
What the difference between jQuery's functions val() and text() ? Where would you use
What is the difference between read() and readline() in C#? Maybe we don't use

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.