Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • 1
    What about objects in function scope that are also closed over? The statement "executes and ends, all of the variables will be garbage collected when the function is over" is incorrect. Commented Sep 23, 2014 at 21:11
  • Local objects are garbage collected when the function scope is over, no? Commented Sep 23, 2014 at 21:12
  • No. That's just not how it works. The garbage collection algorithms used by browsers take varying routes, but none of them go for "function ended, garbage collect". Closures are a fundamental part of Javascript and GCing function scoped variables at function end time would decimate this concept. Commented Sep 23, 2014 at 21:16
  • Isn't a closure and the inner part of a function basically the same thing? Commented Sep 23, 2014 at 21:18
  • 1
    All modern browsers use the Mark-and-sweep algorithm for GC. It's not enough to do reference counting. You can read more about this here: developer.mozilla.org/en-US/docs/Web/JavaScript/… Commented Sep 23, 2014 at 21:20