JavaScript Closure
A short introduction into understanding closure
I recently started learning JavaScript and I have to say that at the beginning it was really difficult to picture what was going. Coming from having learned ruby before I had trouble adjusting to it, but within a couple of days I started to get the hang of it(full disclosure: some of it). One of the main concepts that felt difficult to grasp was the idea of closure but as I started practicing and implementing closure it really made it possible for me to start thinking about building things more dynamically.
Get Jorge Guillén’s stories in your inbox
Join Medium for free to get updates from this writer.
Closure is multiple functions bundled together within another function. Each function within being able to have access to its outer scope and the outer scopes of its parent function.
In the above diagram you can see that the gray function represents a parents function with two functions within it. These two functions have access its parents function scopes but not to each others interior scope. The scope within closure works from the inside out not from the outside in.
Want to read this story later? Save it in Journal.
Closure allows you to create an environment within a function. The children function being able to have access to the parents function scopes and variables even after it has been executed. As you may imagine this will allow you to create more dynamic applications and allow you to be creative with how you use the access of these scopes. For examples maybe one of the functions has a callback or event listener that is executed once something is clicked. While existing within that environment it will have access to the scope and variables of the parent scope. I know this may seem confusing at first and I hope the diagram above helps clear up a visual image for you. Understanding closure will be pivotal to learning how build and grow with JavaScript. I look forward to my own progress and I hope I was able to shed some insight to any other beginners!
📝 Save this story in Journal.
👩💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. Read the Noteworthy in Tech newsletter.






