What is closure...

Table of contents
Introduction
Have you ever wondered how some functions in JavaScript seem to 'remember' values long after they were created? It’s almost as if functions pack a little piece of their environment into a backpack to carry wherever they go. This process, known as a closure, is one of JavaScript's most interesting concepts and I’ll admit—it confused me for a while until I understood closures as more of an effect than a tangible 'thing' in code.
What are Closures?
At its core, a Closure is when a function retains access to variables from its outer scope, even after that outer scope has finished executing. It’s like a function carrying a “backpack” of variables it might need later on. Imagine you create a function inside another function.
Even after the outer function is done running, the inner function can still access the variables from the outer scope. That’s a closure in action!
To better understand this, it helps to think about how JavaScript handles scope, here’s a helpful link for context (https://developer.mozilla.org/en-US/docs/Glossary/Scope) with credit to MDN.
Imagine you’re prepping for a hiking trip, gathering all the essential items you might need like your water bottle, snacks, map, and flashlight. Once you set out, your trusty backpack carries everything with you. When you're thirsty, you can easily grab your water bottle. In this analogy, the backpack is like the function you wrote, and the closure is all the items packed inside variables and values from the outer scope that you can access anytime.
Caption: The backpack represents the function, and closures are the items packed inside.
Function demonstrating closure
Whenever you create a function in JavaScript, it forms its own lexical environment (essentially the scope it has access to). This environment includes all the variables it has access to, including those in its outer scope. When a function is returned or used elsewhere, this environment comes along with it—that’s the power of closures.
- The greet function creates a variable message and defines an inner function sayHello.
- When greet is called, it returns the sayHello function.
- Even though greet has finished running, the sayHello function still has access to the message variable, thanks to Closure.
Why are closures important?
Closures aren’t just a tricky idea—they’re a key feature of JavaScript that allows you to:
- Data Encapsulation: Hide variables from direct access, but still allow them to be used through specific functions
- State Management: Save and update variable values over time, like keeping track of changes or progress.
- Flexibility in Function Design: Create adaptable and reusable code by using closures in different kinds of functions.
By mastering closures, you can make hard tasks easier, write clearer code, and come up with creative ways to solve problems.
Conclusion
Closures are an essential part of JavaScript that make functions more powerful and flexible. They allow you to keep variables accessible even after their scope has finished, opening up endless possibilities for solving coding challenges. With a little practice, closures can become a valuable tool in your programming journey. Happy coding!
Subscribe to my newsletter
Read articles from Daisy directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Daisy
Daisy
I'm a Tech student trying to understand the world of coding.