

Closure in JS and why you should bother.
source link: https://dev.to/tanishqsingla/closure-in-js-and-why-you-should-bother-3ch8
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Overview
If you're in middle of learning JavaScript or you already completed various online tutorials, you may have came across closures and did not bother much after learning about it. Interestingly you can easily find tons of blogs on this platform explaining closures in depth, but why you should bother reading them? This blog attempts to answer that.
This blog is an attempt to make you curious and give you a feel why closures are considered one of the pillars in JavaScript.
How tutorials do it?
If you're watching one of the tutorials or you've enrolled in a boot camp that briefs JavaScript, they might have touched the concept of closure and there's a high chance you've came across a code like this.
const string = "hello closure";
function print() {
console.log(string);
}
Which outputs:
hello closure
And a lazy conclusion comes out of this example is that the function insides can get the variable from its parent scope and marking the end of story for closures.
Does it really end there?
This part of the blog is a bit sloppy, so bear with me a little.
I don't remember the itihās (history of events) and what was I doing, I wanted to create a function that only ran once no matter how many times you call it but I didn't want it to use a global variable that keeps tracks of the execution count. So I came up with a higher order function, it looked like this:
function closure() {
let counter = 0;
function onlyExecutesOnce() {
if(counter == 1) {
return;
}
counter += 1;
console.log(counter);
}
return onlyExecutesOnce;
}
/* testing the function */
const myFunction = closure();
myFunction();
myFunction();
myFunction();
Output:
At first glance I didn't not bother much and to my eyes it looked plain and simple.
So What's the Conclusion?
So, after thinking about my code a question dropped in my mind.
Why is it that myFunction()
is sharing the same surrounding environment as onlyExecutesOnce()
?
This question puzzled me as I wasn't able to get to a straight forward explanation for this behavior. After reading about it I came to know that whenever you pass a function definition it also takes its surrounding information with it.
And that's what closures are, the function is not only getting the variables from its surrounding it is also attaching itself with the information from its surrounding of where it was declared. In this case onlyExecutesOnce()
is also attached with information of its surrounding which is being passed to myFunction()
.
So closures, my fellow readers opens a lots of doors for doing crazy and cool stuff in JavaScript.
Recommend
-
31
This week, I was invited to give a talk at Questers about the topic - Why should we bother present...
-
4
Is it new? If not, don’t bother posting it. Whether you’re doing a...
-
8
If iPad Pro could run MacOS, would you bother with iPadOS? I would almost always use MacOS Votes: 14 15.6% I would al...
-
7
Why Bother C...
-
4
Personal growth and transformation Should You Even Bother with New Year’s Resolutions This Year? ...
-
6
As Atomic’s marketing director, one question I’m most often asked is how we get our employees to write for a daily blog. In this (meta) blog post series, I’ll answer that and other questions related to blogging in case it helps your company try ou...
-
9
-
6
Did anyone skip the iPhone 13 model because it’s unlucky number? Yes I did skip it because of that Votes: 0 0.0% It crossed my mind b...
-
7
...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK