DEV Community

Cover image for Nail your interview by learning Closure (backpack analogy)!
Paul Ryan
Paul Ryan

Posted on

6

Nail your interview by learning Closure (backpack analogy)!

'Can you explain Closure?'

Be prepared to hear this in almost all technical JavaScript interviews, it is one of the core questions they ask you in order to see how well you know JavaScript. You probably use closure daily when writing JavaScript but the majority of the time this is unknown to you.

To get into closure, let's look at a regular function:

     function helloWorld() {
          return "hello world"
     }

     const myFunction = helloWorld;
     myFunction()
Enter fullscreen mode Exit fullscreen mode

This may not be the most inspirational code but if we delve a little bit deeper there is actually quite a bit going on.

     // add the label helloWorld into our global memory with the value of the function
     function helloWorld() {
          return "hello world"
     }
     // now add the label myFunction into our global memory 
     // with the value of the helloWorld label
     const myFunction = helloWorld;
     // call myFunction which will call the helloWorld function and create a new 
    //  execution context for helloWorld which will end when the return statement is met
     myFunction()
Enter fullscreen mode Exit fullscreen mode

If you do not know what an execution context is then head here where I go in-depth.

The most important thing to note is that when we call a function, we create a new execution context for that function which we push onto the call stack then when we meet a return or the last } we pop if from the call stack.

Take the example below where the addOne function is removed from the call stack when it hits a return statement.

Call Stack Example

With an understanding of the call stack & execution context in place, we can now start with closure.

Let's take the classic counter example.

     function outer() {
       let counter = 0;
       function incrementCounter() {
         return counter++;
       }

       return incrementCounter;
     }

     const myCoolFunction = outer();
     console.log(myCoolFunction()) // 0
     console.log(myCoolFunction()) // 1
     console.log(myCoolFunction()) // 2
     console.log(myCoolFunction()) // 3
Enter fullscreen mode Exit fullscreen mode

This kinda breaks what we were saying above, how is the counter value being persisted on each function call? We create a new execution context for each call so should this value not be essentially reset on each function call?

The magic is happening on this line:

const myCoolFunction = outer();
Enter fullscreen mode Exit fullscreen mode

myCoolFunction is essentially getting two values here - the incrementCounter function and also the counter variable.

Just imagine that the incrementCounter function has a backpack with the values around it, so myCoolFunction is assigned the incrementCounter function and also it's backpack (thank you Will Sentence for this analogy).

Here is a beautiful image of that:
Backpack Image

There are few different names rather than backpack which sound a lot more fancy which are:

  • Persistent Lexical Scope Referenced Data (rolls off the tongue?)
  • Closed Over 'Variable Environment'

The above definitions will make you sound super smart in an interview, but for understanding closure just think of the backpack!

To go further in-depth with closure, here is a great article that also uses the backpack analogy.

Conclusion

Any questions on the above, feel free to contact me on my socials! ❣️

πŸ’‚β€β™‚οΈ Insta πŸ‘€ Twitter πŸ’¬ Github πŸ’­ LinkedIn πŸ’₯ Youtube πŸ“­ Website

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (2)

Collapse
 
secure_daily profile image
Artem β€’ β€’ Edited

Great example, and detailed explanation! Glad to see JS developers using closures, and explaining the concept to others!

Collapse
 
paulryan7 profile image
Paul Ryan β€’

Thanks very much Artem

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more