Professional Documents
Culture Documents
Understand Closures in
through your day.
Follow
JavaScript
1.6K 20 Closures don’t have to be complicated. Learn and understand the
basics of closures in just 10 minutes.
What’s a closure?
Closures are a key aspect of JavaScript that every developer should know
about and understand. Today’s article merely skims the surface of closures,
but will give you a good idea of what closures are and how they work in
JavaScript. Lets jump in…
Definition #1:
A closure is a function that has access to the parent scope, even after the scope
has closed.
Definition #2:
Contrast that to this example where we define words in the global scope.
This means it’s accessible to every function in the document:
Nested Functions
What happens when we nest one function inside of another? I want you to
follow along with this next example because this is where it gets fun!
Cool. Now copy and paste the below code into your console. All we’ve done
is create a function named speak . speak returns a function named logIt .
And finally all logIt does is log the value of words to the console, in this
case that means it logs 'hi' to the console.
1 function speak() {
2 return function logIt() {
3 var words = 'hi';
4 console.log(words);
5 }
6 }
Once you’ve got that copied into your console, we’re going to create a
variable and assign it to our speak function as so:
Now we can see what the value of sayHello is by calling the variable but not
invoking the inner function:
sayHello;
// function logIt() {
// var words = 'hi';
// console.log(words);
// }
sayHello();
// 'hi'
It works! But this isn’t anything special. Let’s move one line of code and see
what changes. Take a look at the example below. We’ve moved our
declaration of the variable words outside of the inner function and into the
speak() function:
1 function speak() {
2 var words = 'hi';
3 return function logIt() {
4 console.log(words);
5 }
6 }
Like before, lets declare a variable and assign it to our speak function:
sayHello
// function logIt() {
// console.log(words);
// }
sayHello();
// 'hi'
It still works! And that’s because you’ve just experienced the effects of a
closure!
A closure is a function that has access to the parent scope, even after the scope
has closed.
In this case our speak() function’s scope has closed. This means the var
words = 'hi' should also be gone. However, in JavaScript we have this cool
little concept called closures: Our inner function maintains a reference to Top highlight
the scope in which it was created. This allows the logIt() function to still
access the words variable — even after speak() has closed.
1 function speak() {
2 var words = 'hi';
3 return function logIt() {
4 console.log(words);
5 }
6 }
It’s important to note that every function in JavaScript has a closure. There’s
nothing you need to explicitly do to a function to get this to work. It’s just a
part of JavaScript.
Example #2
Lets examine another example. This one is a little more complex. Here’s the
code:
We’ve got a function name that takes one parameter and returns an
anonymous function that takes a different parameter. The result of the
inner function returns a string.
Let’s create two invocations of the name function. For one we’ll pass in the
name John, and the other, Cindy:
var j = name('John');
var c = name('Cindy');
j;
// function (a) {
// return `${n} likes ${a}`;
// }
Closing Notes
Hopefully you can now understand the basics of closures is in JavaScript
and how they work! This is merely the tip of the iceberg though. You now
have the knowledge to learn about the more complex and practical
examples of closures.
I publish a few articles and tutorials each week, please consider entering
your email here if you’d like to be added to my once-weekly email list.
And when you’re ready to really dive into web development, check out
my Best Courses for Learning Full Stack Web Development
If this post was helpful, please click the clap 👏button below a few
times to show your support! ⬇⬇
1.6K 20
WRITTEN BY
codeburst Follow
Vue with Nuxt.js — show How to make Drag and some things you should Touchpoints - Safe,
GET URL query drop in NativeScript be known as a javascript Secure Integrations
parameters in view Raj Bhatt in ashutec
developer Everywhere!
template or script ( #Nuxt Md Shoharab Pk James Andrew Vaughn in
#Vue ) MindTouch Product and
Yuma Inaura いなうらゆうま 稲 Engineering
浦悠馬 @yumainaura
Dark Mode in React with Drawing rectangle on Sending Email With Preparing to Learn
localStorage video using canvas and Firebase Functions JavaScript
Thomas Weld in The Startup
FabicJs [Firestore & HTTP Aidan McBride
Deepak Rai
Triggers]
Raja Tamil