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

  • Home
  • SEARCH
  • 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 42161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:17:41+00:00 2026-05-10T15:17:41+00:00

How would you explain JavaScript closures to someone with a knowledge of the concepts

  • 0

How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves?

I have seen the Scheme example given on Wikipedia, but unfortunately it did not help.

  • 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. 2026-05-10T15:17:41+00:00Added an answer on May 10, 2026 at 3:17 pm

    A closure is a pairing of:

    1. A function and
    2. A reference to that function’s outer scope (lexical environment)

    A lexical environment is part of every execution context (stack frame) and is a map between identifiers (i.e. local variable names) and values.

    Every function in JavaScript maintains a reference to its outer lexical environment. This reference is used to configure the execution context created when a function is invoked. This reference enables code inside the function to "see" variables declared outside the function, regardless of when and where the function is called.

    If a function was called by a function, which in turn was called by another function, then a chain of references to outer lexical environments is created. This chain is called the scope chain.

    In the following code, inner forms a closure with the lexical environment of the execution context created when foo is invoked, closing over variable secret:

    function foo() {   const secret = Math.trunc(Math.random() * 100)   return function inner() {     console.log(`The secret number is ${secret}.`)   } } const f = foo() // `secret` is not directly accessible from outside `foo` f() // The only way to retrieve `secret` is to invoke `f`

    In other words: in JavaScript, functions carry a reference to a private "box of state", to which only they (and any other functions declared within the same lexical environment) have access. This box of the state is invisible to the caller of the function, delivering an excellent mechanism for data-hiding and encapsulation.

    And remember: functions in JavaScript can be passed around like variables (first-class functions), meaning these pairings of functionality and state can be passed around your program, similar to how you might pass an instance of a class around in C++.

    If JavaScript did not have closures, then more states would have to be passed between functions explicitly, making parameter lists longer and code noisier.

    So, if you want a function to always have access to a private piece of state, you can use a closure.

    …and frequently we do want to associate the state with a function. For example, in Java or C++, when you add a private instance variable and a method to a class, you are associating the state with functionality.

    In C and most other common languages, after a function returns, all the local variables are no longer accessible because the stack-frame is destroyed. In JavaScript, if you declare a function within another function, then the local variables of the outer function can remain accessible after returning from it. In this way, in the code above, secret remains available to the function object inner, after it has been returned from foo.

    Uses of Closures

    Closures are useful whenever you need a private state associated with a function. This is a very common scenario – and remember: JavaScript did not have a class syntax until 2015, and it still does not have a private field syntax. Closures meet this need.

    Private Instance Variables

    In the following code, the function toString closes over the details of the car.

    function Car(manufacturer, model, year, color) {   return {     toString() {       return `${manufacturer} ${model} (${year}, ${color})`     }   } }  const car = new Car('Aston Martin', 'V8 Vantage', '2012', 'Quantum Silver') console.log(car.toString())

    Functional Programming

    In the following code, the function inner closes over both fn and args.

    function curry(fn) {   const args = []   return function inner(arg) {     if(args.length === fn.length) return fn(...args)     args.push(arg)     return inner   } }  function add(a, b) {   return a + b }  const curriedAdd = curry(add) console.log(curriedAdd(2)(3)()) // 5

    Event-Oriented Programming

    In the following code, function onClick closes over variable BACKGROUND_COLOR.

    const $ = document.querySelector.bind(document) const BACKGROUND_COLOR = 'rgba(200, 200, 242, 1)'  function onClick() {   $('body').style.background = BACKGROUND_COLOR }  $('button').addEventListener('click', onClick)
    <button>Set background color</button>

    Modularization

    In the following example, all the implementation details are hidden inside an immediately executed function expression. The functions tick and toString close over the private state and functions they need to complete their work. Closures have enabled us to modularize and encapsulate our code.

    let namespace = {};  (function foo(n) {   let numbers = []    function format(n) {     return Math.trunc(n)   }    function tick() {     numbers.push(Math.random() * 100)   }    function toString() {     return numbers.map(format)   }    n.counter = {     tick,     toString   } }(namespace))  const counter = namespace.counter counter.tick() counter.tick() console.log(counter.toString())

    Examples

    Example 1

    This example shows that the local variables are not copied in the closure: the closure maintains a reference to the original variables themselves. It is as though the stack-frame stays alive in memory even after the outer function exits.

    function foo() {   let x = 42   let inner = () => console.log(x)   x = x + 1   return inner }  foo()() // logs 43

    Example 2

    In the following code, three methods log, increment, and update all close over the same lexical environment.

    And every time createObject is called, a new execution context (stack frame) is created and a completely new variable x, and a new set of functions (log etc.) are created, that close over this new variable.

    function createObject() {   let x = 42;   return {     log() { console.log(x) },     increment() { x++ },     update(value) { x = value }   } }  const o = createObject() o.increment() o.log() // 43 o.update(5) o.log() // 5 const p = createObject() p.log() // 42

    Example 3

    If you are using variables declared using var, be careful you understand which variable you are closing over. Variables declared using var are hoisted. This is much less of a problem in modern JavaScript due to the introduction of let and const.

    In the following code, each time around the loop, a new function inner is created, which closes over i. But because var i is hoisted outside the loop, all of these inner functions close over the same variable, meaning that the final value of i (3) is printed, three times.

    function foo() {   var result = []   for (var i = 0; i < 3; i++) {     result.push(function inner() { console.log(i) } )   }    return result }  const result = foo() // The following will print `3`, three times... for (var i = 0; i < 3; i++) {   result[i]()  }

    Final points:

    • Whenever a function is declared in JavaScript closure is created.
    • Returning a function from inside another function is the classic example of closure, because the state inside the outer function is implicitly available to the returned inner function, even after the outer function has completed execution.
    • Whenever you use eval() inside a function, a closure is used. The text you eval can reference local variables of the function, and in the non-strict mode, you can even create new local variables by using eval('var foo = …').
    • When you use new Function(…) (the Function constructor) inside a function, it does not close over its lexical environment: it closes over the global context instead. The new function cannot reference the local variables of the outer function.
    • A closure in JavaScript is like keeping a reference (NOT a copy) to the scope at the point of function declaration, which in turn keeps a reference to its outer scope, and so on, all the way to the global object at the top of the scope chain.
    • A closure is created when a function is declared; this closure is used to configure the execution context when the function is invoked.
    • A new set of local variables is created every time a function is called.

    Links

    • Douglas Crockford’s simulated private attributes and private methods for an object, using closures.
    • A great explanation of how closures can cause memory leaks in IE if you are not careful.
    • MDN documentation on JavaScript Closures.
    • The Beginner’s Guide to JavaScript Closures.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Figured this one out on my own moments ago. Turns… May 11, 2026 at 11:51 pm
  • Editorial Team
    Editorial Team added an answer You don't necessarily have to delete "old" data all the… May 11, 2026 at 11:51 pm
  • Editorial Team
    Editorial Team added an answer Although it looks like it, for(int i = 0; i… May 11, 2026 at 11:51 pm

Related Questions

Where X is any programming language (C#, Javascript, Lisp, Perl, Ruby, Scheme, etc) which
As a jQuery neophyte I am somewhat confused by the different contexts in which
I'm considering porting a very simple text-templating library to scala, mostly as an exercise
Here is my question, Would it be possible, knowing that classic asp support server-side

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.