DEV Community

Cover image for Mastering JavaScript Closures: A Comprehensive Guide

Mastering JavaScript Closures: A Comprehensive Guide

MD. JAHID HOSSAIN on June 10, 2024

JavaScript closures are a powerful yet sometimes misunderstood concept in JavaScript programming. Despite being a bit tricky to understand at first...
Collapse
 
jonrandy profile image
Jon Randy 🎖️

A closure in JavaScript is created when a function is defined within another function...

Unfortunately, this is not correct. Nesting functions has nothing to do with it. A closure is created every time a function is created, at the time of that function's creation.

Collapse
 
jahid6597 profile image
MD. JAHID HOSSAIN

Thanks for the feedback. You're right that closures are created every time a function is created. However, my definition was emphasizing the common scenario where an inner function retains access to the outer function's variables even after the outer function has executed, highlighting the practical use of closures in nested functions.

Collapse
 
chibuzor_6a8bc8258 profile image
Chibuzor Israel

Is a closure really created every time a function is created?
Creating functions and closures are not the same.

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Yes. EVERY function has an associated closure, that is created when you create the function. There is no 'manual' way to create a closure, it happens automatically.

Thread Thread
 
chibuzor_6a8bc8258 profile image
Chibuzor Israel

I just researched about your response, and you seem to be correct. This brings about certain confusion for me because now I think I don't know what I knew.
Why is a closure typically described as a function with reference to free variables? are you saying functions without free variables , functions with free variables are all closures?

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Why is a closure typically described as a function with reference to free variables?

Probably because there's a lot of incorrect information out there, or possibly because it is believed to be a simpler way to explain it (despite being wrong).

...are you saying functions without free variables , functions with free variables are all closures?

I'm saying closures ARE NOT functions. They are the mechanism by which functions retain access to variables in the lexical scope in which they were created. Every function has one. I hope this makes sense.

Also, I'm not sure what you mean by 'free variables'.

Collapse
 
neeraj_kumar_13135bb56efe profile image
Neeraj Kumar

What a blog i clearly understand all these thing from here ,thank so much

Collapse
 
jahid6597 profile image
MD. JAHID HOSSAIN

Thank you! I'm glad to hear that

Collapse
 
owolabiyusufojo profile image
Owolabi Yusuf

Thank you for your clear and concise explanations.your effort is highly appreciated !

Collapse
 
jahid6597 profile image
MD. JAHID HOSSAIN

Thank you for your kind words!

Collapse
 
berkanserbes profile image
Berkan Serbes

Well explained 👍

Collapse
 
jahid6597 profile image
MD. JAHID HOSSAIN

Thank you! Glad you liked it! 👍

Collapse
 
mr_frank profile image
Chibuike Franklin

❤️👏

Collapse
 
jahid6597 profile image
MD. JAHID HOSSAIN

❤️