Closure In JavaScript Explained In Five Minutes

A small feature that solves many not-so-small problems

Peterson C
The Startup

--

Photo by Christopher Gower on Unsplash

“Can you explain CLOSURE in javascript and how you would use it?” is a question that came up in almost all of the interviews I’ve had. It doesn’t matter whether it was a front-end or backend position. Somehow, knowing what a closure was and its usages seemed necessary. Sometimes, the person that was interviewing me and asking this question didn’t even understand what closure was. The goal of this article is to demystify CLOSURE. It’s a simple, yet useful concept once you understand it. Most front-end developers have used it before whether it be consciously or unconsciously.

A closure is simply a function that still has access to its parents’ private members (scope) even after the parent function has been closed.

Well, how do we use it now that we know what it is? Let’s implement a feature where closure is the best solution I can think of right now.

You’ve been given the honor of creating the most beautiful site ever. One of the most wonderful features of the site is the ability for visitors to click the famous “High-Five” button. When a user is really happy with the site content, the button gets clicked…

--

--

Peterson C
The Startup

Husband, father, engineer, musician, and writer by luck. I write mostly about whatever comes to mind. Follow me on this crazy writing journey if you dare.