Why Can a Function Remember a Variable That Should Be Long Gone?
📰 Medium · Programming
Learn how JavaScript functions can remember variables even after their execution context is cleaned up, and understand the concept of closures
Action Steps
- Define a function inside another function to create a closure
- Use the inner function to access variables from the outer function's scope
- Test the closure by calling the inner function outside of the outer function's execution context
- Apply closures to real-world problems, such as creating private variables or functions
- Compare the use of closures with other programming concepts, such as classes or modules
Who Needs to Know This
Developers working with JavaScript will benefit from understanding closures, as it can help them write more efficient and effective code
Key Insight
💡 Closures allow functions to access variables from their outer scope, even after the outer function has returned
Share This
Discover how #JavaScript functions can remember variables even after they're gone, thanks to #closures!
Key Takeaways
Learn how JavaScript functions can remember variables even after their execution context is cleaned up, and understand the concept of closures
Full Article
Title: Why Can a Function Remember a Variable That Should Be Long Gone?
URL Source: https://medium.com/@gize.bonilla/why-can-a-function-remember-a-variable-that-should-be-long-gone-35f4192fc25d?source=rss------programming-5
Published Time: 2026-04-27T03:12:21Z
Markdown Content:
# Why Can a Function Remember a Variable That Should Be Long Gone? | by Gize Bonilla | Apr, 2026 | Medium
[Sitemap](https://medium.com/sitemap/sitemap.xml)
[Open in app](https://play.google.com/store/apps/details?id=com.medium.reader&referrer=utm_source%3DmobileNavBar&source=post_page---top_nav_layout_nav-----------------------------------------)
Sign up
[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40gize.bonilla%2Fwhy-can-a-function-remember-a-variable-that-should-be-long-gone-35f4192fc25d&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)
[](https://medium.com/?source=post_page---top_nav_layout_nav-----------------------------------------)
Get app
[Write](https://medium.com/m/signin?operation=register&redirect=https%3A%2F%2Fmedium.com%2Fnew-story&source=---top_nav_layout_nav-----------------------new_post_topnav------------------)
[Search](https://medium.com/search?source=post_page---top_nav_layout_nav-----------------------------------------)
Sign up
[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40gize.bonilla%2Fwhy-can-a-function-remember-a-variable-that-should-be-long-gone-35f4192fc25d&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)

# Why Can a Function Remember a Variable That Should Be Long Gone?
## Closures are the payoff of everything this series has built so far. They are not advanced magic — they are the natural result of how JavaScript handles scope
[](https://medium.com/@gize.bonilla?source=post_page---byline--35f4192fc25d---------------------------------------)
[Gize Bonilla](https://medium.com/@gize.bonilla?source=post_page---byline--35f4192fc25d---------------------------------------)
Follow
5 min read
·
Just now
[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fvote%2Fp%2F35f4192fc25d&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40gize.bonilla%2Fwhy-can-a-function-remember-a-variable-that-should-be-long-gone-35f4192fc25d&user=Gize+Bonilla&userId=ac116ccdd068&source=---header_actions--35f4192fc25d---------------------clap_footer------------------)
[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fbookmark%2Fp%2F35f4192fc25d&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40gize.bonilla%2Fwhy-can-a-function-remember-a-variable-that-should-be-long-gone-35f4192fc25d&source=---header_actions--35f4192fc25d---------------------bookmark_footer------------------)
[Listen](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2Fplans%3Fdimension%3Dpost_audio_button%26postId%3D35f4192fc25d&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40gize.bonilla%2Fwhy-can-a-function-remember-a-variable-that-should-be-long-gone-35f4192fc25d&source=---header_actions--35f4192fc25d---------------------post_audio_button------------------)
Share
Press enter or click to view image in full size

Photo by Chalana Nilupul on [Unsplash](https://unsplash.com/?utm_source=medium&utm_medium=referral)
A function runs and returns. Its execution context is cleaned up, its variables released, its room demolished. And then, somewhere else in your code, later, another function uses one of those variables. The room is gone. The variable is not. If you have ever wondered how that is possible, this is the episode the series was building to
URL Source: https://medium.com/@gize.bonilla/why-can-a-function-remember-a-variable-that-should-be-long-gone-35f4192fc25d?source=rss------programming-5
Published Time: 2026-04-27T03:12:21Z
Markdown Content:
# Why Can a Function Remember a Variable That Should Be Long Gone? | by Gize Bonilla | Apr, 2026 | Medium
[Sitemap](https://medium.com/sitemap/sitemap.xml)
[Open in app](https://play.google.com/store/apps/details?id=com.medium.reader&referrer=utm_source%3DmobileNavBar&source=post_page---top_nav_layout_nav-----------------------------------------)
Sign up
[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40gize.bonilla%2Fwhy-can-a-function-remember-a-variable-that-should-be-long-gone-35f4192fc25d&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)
[](https://medium.com/?source=post_page---top_nav_layout_nav-----------------------------------------)
Get app
[Write](https://medium.com/m/signin?operation=register&redirect=https%3A%2F%2Fmedium.com%2Fnew-story&source=---top_nav_layout_nav-----------------------new_post_topnav------------------)
[Search](https://medium.com/search?source=post_page---top_nav_layout_nav-----------------------------------------)
Sign up
[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2F%40gize.bonilla%2Fwhy-can-a-function-remember-a-variable-that-should-be-long-gone-35f4192fc25d&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)

# Why Can a Function Remember a Variable That Should Be Long Gone?
## Closures are the payoff of everything this series has built so far. They are not advanced magic — they are the natural result of how JavaScript handles scope
[](https://medium.com/@gize.bonilla?source=post_page---byline--35f4192fc25d---------------------------------------)
[Gize Bonilla](https://medium.com/@gize.bonilla?source=post_page---byline--35f4192fc25d---------------------------------------)
Follow
5 min read
·
Just now
[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fvote%2Fp%2F35f4192fc25d&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40gize.bonilla%2Fwhy-can-a-function-remember-a-variable-that-should-be-long-gone-35f4192fc25d&user=Gize+Bonilla&userId=ac116ccdd068&source=---header_actions--35f4192fc25d---------------------clap_footer------------------)
[](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2F_%2Fbookmark%2Fp%2F35f4192fc25d&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40gize.bonilla%2Fwhy-can-a-function-remember-a-variable-that-should-be-long-gone-35f4192fc25d&source=---header_actions--35f4192fc25d---------------------bookmark_footer------------------)
[Listen](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2Fplans%3Fdimension%3Dpost_audio_button%26postId%3D35f4192fc25d&operation=register&redirect=https%3A%2F%2Fmedium.com%2F%40gize.bonilla%2Fwhy-can-a-function-remember-a-variable-that-should-be-long-gone-35f4192fc25d&source=---header_actions--35f4192fc25d---------------------post_audio_button------------------)
Share
Press enter or click to view image in full size

Photo by Chalana Nilupul on [Unsplash](https://unsplash.com/?utm_source=medium&utm_medium=referral)
A function runs and returns. Its execution context is cleaned up, its variables released, its room demolished. And then, somewhere else in your code, later, another function uses one of those variables. The room is gone. The variable is not. If you have ever wondered how that is possible, this is the episode the series was building to
DeepCamp AI