Skip to content
Lydia Hallie theavocoder
Course contents
Complete JavaScript / Lesson 19

Closures

Every execution context has their own memory space set up. This memory space can contain the values of the passed arguments, or variables defined within that function. This means that even after that certain function might have returned and popped off the stack, we still have access to its memory for a while!

Closures diagram 1

When we invoked sayHi, we added the greet function to the call stack.

Closures diagram 2

The greet function also returns a function! This function receives an argument, that we later pass when we invoke the sayHi function.

As the greet function returns, it gets popped off the stack. However, its memory space is still available for a while. Then, sayHi gets invoked and pushed to the stack.

Closures diagram

In sayHi, we need to have access to the greeting variable. It can’t find that within that function, so it goes up the scope chain. When its code is invoked, and it sees the greeting variable, it goes up the scope chain, and there is an outer lexical environment reference to that variable. Although the execution context of greet is gone, the execution context of sayHi still has a reference to the memory space of its outer environment.

Closures diagram

A closure is a function having access to the parent scope, even after the parent function has been popped off the stack. A closure makes sure that it “closes in” all the variables that it should have have access to, hence the name closure.

References and further reading