The Infamous Interview QuestionThese days, I am actively applying to and interviewing for jobs. Maybe you are too, dear reader.A few days ago, I had an interview in which the engineering team asked me to explain what a closure is.
Its certainly not the first time that Ive been asked about the term, but Im not going to lie, I panicked a bit. Closures have developed something of an infamous reputation for being an impossibly difficult screening question.After the interview, I was frustrated that the topic was still daunting to me.
I was determined to buckle down and defeat closure once and for all. This blog post walks you through my journey.Anonymous Functions and IIFEs Are Not ClosuresTo start this post, I want to clarify what I will not be writing about.
Anonymous functions are also not closures. anonymousFunc !== closure && IIFE !
== closure // trueIt is still valuable to learn about these use cases. If you can understand how closure was used in the past, you will understand how its used in the present.Not to mention there is a lot of ES5 legacy code out there.
However, that is not what Ill be covering here today. Now that weve clarified, lets dive in.Closure as a ConceptIn computer science, a closure is a function that has an environment of its own and at least one variable within that environment.
MDN goes on to say:A closure is the combination of a function bundled together (enclosed) with references to its surrounding state.This brings us to scope.Whats Scope Got to Do With ItLets dig a little deeper into the term surrounding state from the previous quote.
You might think of global scope as a country. Within a country, there are many cities, each enclosed within its own borders. Similarly, within specific parts of your program, we will find objects enclosed with local scope.
Similarly, if you enclose a variable within curly braces anywhere in your code, that variable will also be cut off, it is block scoped.Closure and ScopeIt may be helpful to think of closure as a sentient or self-aware door enclosing a function. For example, when we create a new function, that functions closure looks around and takes note of its surroundings, its scope.
function highestBoxOffice() const context = The highest grossing movie of all time is ; return context Avengers: Endgame;Even though this function has no children, it still has closure. Closure does not exist only in nested functions. This functions closure looks around and sees the variables exist inside of it, in this case, the variable context.
Closure in Nested FunctionsIf we create a nested function, that functions closure sees the walls of the parent function it exists inside of. The parent functions scope is the nested functions outer scope. This includes variables inside of its parent function.
This is where closures really come in handy. Our function genreTopGross() has a closure. Its closure looks inward and sees its own inner scope which encloses return Math.
Max(movies. genre. boxOffice).
It also looks outward and sees its outer scope, notating that it is inside a function, highestBoxOffice(). It also sees and has access to any arguments passed into its parent function. Lets pass an argument in now.
As you can see, we have declared a new variable, topGrossing()and we have assigned it the value of highestBoxOffice(domesticMoviesObj). Currently, topGrossing would be undefined, but now we take our next step:We invoke topGrossing() and pass Romantic Comedy as an argument. Now we see closure in action!
What does this have to do with closure?Closure only provides access from inner to outer scope, not from outer to inner scope.So, if you declare and define a variable deep within several nested functions, but use it in the outer scope of a parent function, the compiler will return an undefined error.
Remember, the car only drives in reverse.ConclusionAs you can see, understanding closures requires a solid understanding of functions, scope, and the scope chain, which is exactly what employers are looking for when they ask this question.This blog covers what closures are, but not their many use cases.
If you understood this, you should be able to dive deeper into those use cases without feeling totally lost.If nothing else, I hope this provides a simple foundation or recap to keep you from panicking about closures.As always, thanks for reading.
Now go ace that interview!