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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:21:08+00:00 2026-06-14T17:21:08+00:00

As many as Articles as i’ve read , I still have some questions. I

  • 0

As many as Articles as i’ve read , I still have some questions.

I already know (and understand) the usages of closure like :

  1. The Infamous Loop Problem ( loop of links with alert on each one of them with kept number)

  2. increased counter (keep calling a function -> which alerts increased numbers)

From here :

inner functions referring to local variables of its outer function
create closures

From here :

a closure is the local variables for a function – kept alive after the
function has returned, or a closure is a stack-frame which is not
deallocated when the function returns. (as if a ‘stack-frame’ were
malloc’ed instead of being on the stack!)

3 Questions please:

Question #1

I was told that all functions in JS create Closures.

WHAT ??? if I create a normal function with private variable it just create a scope. not closure.

I thought that closure work like this:(from here)

  1. Make an outer, “function maker” function.
  2. Declare a local variable inside it.
  3. Declare an inner function inside the outer one.
  4. Use the outer function’s variable inside the inner function.
  5. Have the outer function return the inner function
  6. Run the function, and assign its return value to a variable

exmample:

function functionMaker(){
   var x = 1;
   function innerFunction(){
     alert(x);
     x++;
   }
   return innerFunction;
}

So why do they say that every js function create closure ?

Question #2

Does Self-Invoking Functions aka IEFA – Immediately Invoked Function Expression creates a closure ?

looking at

(function (a) {
    alert(a);
})(4);

I don’t see the pattern like the 6 rules from the above :
where exactly Have the outer function return the inner function is here ? I dont see the word return.

Question #3

function myFunction() {
  var myVar = 1;
  var alertMyVar = function () {
    alert('My variable has the value ' + myVar);
  };
  setTimeout(alertMyVar, 1000 * 3600 * 24);
}
myFunction();

Does myFunction was alive for the whole day here ? or it was ended as soon after the setTimeout ? and if so , how did SetTimeOut remembered that value ?

Please help me to get it right.

  • 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-14T17:21:09+00:00Added an answer on June 14, 2026 at 5:21 pm

    All function calls create a closure. The important thing to note is whether the closure lasts beyond the lifetime of the function call.

    If I do an immediately-executed function:

    var value = (function() {
      return 4;
    })();
    

    then a closure is created, but discarded when the function returns. Why? Because there are no surviving references to symbols defined in the closure. But here:

    var value = function() {
      var counter = 0;
      return function() {
        return counter++;
      };
    }();
    

    the closure lives on after the immediately executed function, because it returns another function that, in turn, includes references to the “counter” symbol defined in the original function. Because that returned function is still “alive”, the closure has to be retained by the runtime system.

    Note that here:

    var value = function() {
      return function(a) {
        return "hello " + a;
      };
    }();
    

    even though the outer immediately executed function returns a function, that function doesn’t reference any symbols from the outer one, so there’s no need to preserve the closure.

    I guess what I’m trying to say is that it’s helpful to think about the closure as part of the effect of executing a function, rather than strictly as a static, structural thing. It’s possible to have a function that sometimes creates a persistent closure, but not always.

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

Sidebar

Related Questions

I am trying understand ViewModels deeper and I have read many articles and blogs
I have read so many articles but still don't get it. I have a
I have read many articles regarding layout, but I am still quitely confused. My
I have seen many articles and Questions/Answers Regarding Lock Escalation but following things are
I have two model: User, Article A user can like or dislike many articles,
I read many articles about unsafe functions like strcpy, memcpy, etc. which may lead
Despite reading many articles (some on this board) I am still scratching my head
I have read many articles regarding difference between gridview and repeater. I come to
After reading many articles and some questions on here, I finally succeded in activating
I have read so many articles about integration and yet i did not found

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.