Learn Closures In 7 Minutes

  Рет қаралды 315,848

Web Dev Simplified

Web Dev Simplified

4 жыл бұрын

The most infamous interview question for JavaScript has got to be explaining closures. Because of this it may seem that closures are a complex topic, but in reality the idea of closures is actually pretty simple. All you need to know about closures is they they are a mechanism in JavaScript for handling variable scope. In this video I explain in depth exactly what closures are, how to use them, and why they are important.
🧠 Concepts Covered:
- What closures are
- How closures work in JavaScript
- Why closures are important
- When to use closures
🌎 Find Me Here:
My Blog: blog.webdevsimplified.com
My Courses: courses.webdevsimplified.com
Patreon: / webdevsimplified
Twitter: / devsimplified
Discord: / discord
GitHub: github.com/WebDevSimplified
CodePen: codepen.io/WebDevSimplified
#Closures #WDS #JavaScript

Пікірлер: 379
@HK-th3tu
@HK-th3tu 3 жыл бұрын
SUMMARY: Closure i s "when you have a function defined inside of another function, that inner function has access to the variables and scope of the outer function even if the outer function finishes executing and those variables are no longer accessible outside of that function."
@vladislavglad6596
@vladislavglad6596 3 жыл бұрын
Ha-ha! A truly brilliant explanation by Kyle!
@angelamariasuarezparra7872
@angelamariasuarezparra7872 2 жыл бұрын
Thank you
@ProgrammingwithRakesh
@ProgrammingwithRakesh 2 жыл бұрын
Check out this too - kzbin.info/www/bejne/jWjNYp2ZhNSAqpI
@adeelbukhari
@adeelbukhari Жыл бұрын
Now I finally understand closures. Thanks for this summary.
@jamessmith_321
@jamessmith_321 Жыл бұрын
You should get Nobel price
@scottj6296
@scottj6296 3 жыл бұрын
**If you are confused about his code example, please see my 3 step explanation:** 1. The outerFunction('outside') is called immediately when it is assigned to the variable "newFunction". (That's how it works when a function is assigned to a variable with (), if you didn't know this part, you would very confused). 2. Upon being called, outerFunction returns the function "innerFunction(innerVariable") which re-reassigns our variable "newFunction" to be InnerFunction(innerVariable) instead of outerFunction(outerVariable). 3. Then by calling our variable as newFunction('inside') we are calling innerFunction('inside') which runs the console.log code. The closure is that innerFunction remembers and has access to the outerVariable parameter we originally set with outerFunction('outside'). Thus it is able to console.log both 'outside' and 'inside' even though it was called with just 'inside'. If this helped you please thumbs up so others can see!
@free2idol1
@free2idol1 2 жыл бұрын
thanks... it took a while to understand this concept and your explanation helps me re-confirm that what I understand is true.
@BibeshManandhar
@BibeshManandhar 2 жыл бұрын
Thanks man 👍👍
@tylercode2207
@tylercode2207 2 жыл бұрын
Thanks brother it really helped me
@Corvinade
@Corvinade 2 жыл бұрын
Thanks, I was so confused when I saw that.
@xiaotutu0802
@xiaotutu0802 Жыл бұрын
great explanation,thank you!
@CraigClayton-bk3ze
@CraigClayton-bk3ze 4 жыл бұрын
it's been 70 years and I still don't understand closures
@paujoan401
@paujoan401 4 жыл бұрын
You are not alone.
@surfinbird71
@surfinbird71 4 жыл бұрын
Jump to 6:27
@therealilyaskhan
@therealilyaskhan 4 жыл бұрын
Haha okay one tip for you. If you are smart enough you would understand. Javascipt variables do not holds the values they rather holds the references to the physical memory slots in which values are contained. When variables are returned from a function , it forms a closures because the function is not returning the variables/values, the function is rather returning the references to memory slots .. That's it
@thedevelopergirl
@thedevelopergirl 4 жыл бұрын
try having a look at this once: kzbin.info/www/bejne/kHKWoJqNprOBhsk
@thinkingaloud7925
@thinkingaloud7925 4 жыл бұрын
but javascript is only 23 or 24 yrs old
@canvaapplessons1224
@canvaapplessons1224 3 жыл бұрын
I was confused all along but your last sentence saved me 😁
@AbhaySingh-dd9xc
@AbhaySingh-dd9xc 3 жыл бұрын
Same bro i got confused too
@dhanyakumar8965
@dhanyakumar8965 3 жыл бұрын
Same here
@navrajsandhu8168
@navrajsandhu8168 2 жыл бұрын
same
@centrumsaiyan7623
@centrumsaiyan7623 2 жыл бұрын
That last sentence saved me too.
@SENPAI-mi6sz
@SENPAI-mi6sz 2 жыл бұрын
You saved me lok
@doniaelfouly4142
@doniaelfouly4142 Жыл бұрын
i always come back to this channel whenever i need a clear , simple and straight to the point explanation
@kristofbalinth2071
@kristofbalinth2071 2 жыл бұрын
Finally I funckin' got it. Thanks Kyle. I found myself coming back to your channel more and more when I need to clarify/understand some webdev-related stuff. Will definitely buy at least one of your courses. Keep up the good work.
@techuchiha6510
@techuchiha6510 4 жыл бұрын
Closures are such an interesting topic that I always watch anyone or everyone coming up with an explanation for the same. This was one of the best explanations that I have come across.
@WebDevSimplified
@WebDevSimplified 4 жыл бұрын
Thank you!
@kuei-chinhuang815
@kuei-chinhuang815 3 жыл бұрын
Thank you, Kyle. Your explanation is so good that other people's closure videos starting making sense to me too. You simplify web development so successfully that I always benefit from your channel since I started learning JavaScript!
@WebDevSimplified
@WebDevSimplified 3 жыл бұрын
I am so glad I could help!
@Amey_G18
@Amey_G18 2 жыл бұрын
This closure topic was so difficult to understand at first. I've read it on multiple sites but this video by Kyle is really awesome and easy to understand. Thank you Kyle!
@ganeshbabu6458
@ganeshbabu6458 3 жыл бұрын
Thanks for that Axios example, which no one ever has mentioned in their closure tutorial. We were using closures without even realizing it.
@marcotorres899
@marcotorres899 8 ай бұрын
The last example was amazing! Thank you for taking the time to make this!!
@MrTubs987
@MrTubs987 3 жыл бұрын
I think I FINALLY understand closures. In fact, I found a good reason to use one today in my project. TLDR; When you need to pass a function with no parameters, but the function you want to pass has parameters. BAM, create a closure by putting the variables/parameters you need in the outer function and return a function with no parameters (just remember that returned function has access to the outer function parameters, even after it's done executing).
@christopherortega6257
@christopherortega6257 4 жыл бұрын
Most straight forward easy to understand explanation I've seen. Thanks buddy.
@Zonai_Guy
@Zonai_Guy 3 жыл бұрын
It’s just a child function having access to its parents function
@SquaredbyX
@SquaredbyX 3 жыл бұрын
There's more to it. That's actually lexical scope. Lexical scope comes into play in the concept of Closures when that lexical scope is 'closed' out, hence the term closure. A Closure is when you still have access to a lexical scope that has been closed and the values should have been garbage collected are still available to your closure / function that is still accessing them.
@andreadg5429
@andreadg5429 3 жыл бұрын
@@SquaredbyX to see if I got it right, its like the inner scope creates a copy of the outer one for "personal use", right?
@geralt36
@geralt36 3 жыл бұрын
@@SquaredbyX You really hit it home with that explanation. Thank you!
@kobe248ryant
@kobe248ryant 2 жыл бұрын
So what the use of the closure then?
@Zonai_Guy
@Zonai_Guy 2 жыл бұрын
@@kobe248ryant nothing just use the parent scope in the child scope
@aashayamballi
@aashayamballi 4 жыл бұрын
That was simple, because you made me understand it in a simpler way. Thanks Kyle. 😊
@zachwhite8054
@zachwhite8054 4 жыл бұрын
10-second summary: 6:28
@atulsharma706
@atulsharma706 4 жыл бұрын
OK, i am taking it since i don't understand anything else
@stills2359
@stills2359 3 жыл бұрын
thank you lol
@bohdanartemenko
@bohdanartemenko 4 жыл бұрын
Kyle, thanks to your videos I learned not only JS but English too. I don't know how but you clarified and simplified all info in your videos so well, hope so youtube algorithm make you famous, good luck :)
@WebDevSimplified
@WebDevSimplified 4 жыл бұрын
Thank you so much! I'm really glad I can help.
@snake1625b
@snake1625b 2 жыл бұрын
Great video. I watched this a month before my interview but forgot the concept so I recommend y'all watch it again every now and then
@ahlen10
@ahlen10 3 жыл бұрын
You tried to simplify the concept from the start and made your point clear. Thank you.
@dsvhs93
@dsvhs93 3 жыл бұрын
Kyle you are awesome! Thank you for this! I’m constantly coming to your videos! Trying to learn as much as I can but really trying to understand how things work!
@lucasoliveira-xs5yh
@lucasoliveira-xs5yh 2 жыл бұрын
Man, congrats about your videos. I passed in a interview using your videos about React to learn how to build an app and now I'm using again. Continue this job.
@viniciusborgesdelima2519
@viniciusborgesdelima2519 4 жыл бұрын
Excelent explanation, thank you a lot, I was having trouble trying to understand Closures, and this video fit like a glove!
@GuitarreroDaniel
@GuitarreroDaniel 3 жыл бұрын
The best explanation on KZbin, thank you very much. I don't know why ppl keep complaining about not understanding this.
@iam-Inder
@iam-Inder 4 жыл бұрын
I watched many videos on closures but everybody trying to explain too much and somewhere lose the real thing. Your example and explanation are good. I got the concept. thank you for this video.
@sledge9763
@sledge9763 4 жыл бұрын
This is great, appreciate you not making it more complicated than it needs to be. Thanks!
@brownboyintokyo7368
@brownboyintokyo7368 2 жыл бұрын
You are a legend, always glad to come back to these videos and brush up the fundamentals again
@reidwilliamson6477
@reidwilliamson6477 2 жыл бұрын
Really good content man. Thanks for explaining it clear and concise!
@mubashirali6835
@mubashirali6835 4 жыл бұрын
You made this concept very easy. Thank you Kyle.
@lone-warrior-13
@lone-warrior-13 3 жыл бұрын
i love you and your channel i hadn't seen anyone explaining js closures as clear as you
@BusinessDevelopmentSolution
@BusinessDevelopmentSolution Жыл бұрын
i have tried so many videos on youtube but none of them explained in the way you did, thank you so much for a nice video.
@elhamsalamat772
@elhamsalamat772 Жыл бұрын
Thank you very much Kyle for the nice explanation. Closure was not clear exactly to me until I watched your video, That makes the understanding more easier
@Life.Love.Locs.
@Life.Love.Locs. 3 жыл бұрын
Thanks for this! I watched this after a lecture and watching a few other videos that confused me... I finally got it!
@TheFinalsTV
@TheFinalsTV 3 жыл бұрын
*personal note* newFunction call = (outerFunction + calling it). outerFunction executes, returning innerFunction while passing in "outside" as part of the accessible scope. newFunction then passes "inside" to the now returned innerFunction, calling it and completing the execution. For anyone reading, I have no idea if I'm right but this makes sense to me rn. Analogy note: newFunction is like the first car key turn, it activates the starter (outerFunction) which primes the engine by passing in "outside" and returning innerFunction as ready to ignite. The second turn is newFunction passing in "inside" which sends fuel, igniting the engine (calling innerFunction)
@pardeepporia5367
@pardeepporia5367 3 жыл бұрын
Great job buddy, I like the sequence you presented it in: simple example, better example and then use case.
@athira151292
@athira151292 Жыл бұрын
This explanation was great! Thankyou for the video.
@sebiporini
@sebiporini 3 жыл бұрын
I am on my way to understand this. Thanks a lot for helping me, i think i cant understand this at 100%, but i will not give up. Im learning a lot with you and your youtube channel. You got a new subscriber here. Cheers! And thanks again!
@noideaofhowcallme
@noideaofhowcallme 2 жыл бұрын
Explained very well and a VERY good example! Thanks!
@draicor
@draicor 2 жыл бұрын
Good explanation mate, I subscribed!. tip: You really should start using the semicolons while writing javascript code, and avoid using anonymous functions, you should name those functions even if they are IIFEs or arrow functions, it makes debugging easier since the function name shows up on the errors later..
@atulkamble557
@atulkamble557 2 жыл бұрын
You make everything super easy..! Thanks a lot.
@mruduladdipalli5417
@mruduladdipalli5417 4 жыл бұрын
5:07 - innerfunction() will only have access to those variables of outerfunction() which innerfunction() wants to use it. , when we do console.dir( newFunction ) , we can see [ [Scopes] ] Array , which has Closure Object with the variables that would be used by innerfunction() only , not all variables of outerfunction
@emekatimothyiloba699
@emekatimothyiloba699 Жыл бұрын
Kyle is good when it comes to explanation. Thank you
@s5006
@s5006 4 жыл бұрын
i was learning on Udemy and i got some trouble to understand ...after watching this video it was easy peasy to understand ... thanks so much
@thebrijpatel
@thebrijpatel 2 жыл бұрын
Ooo boy, this helped me understand the concept. Thanks !!
@Anne-kz4fi
@Anne-kz4fi 3 жыл бұрын
Thank you. I think I understood. I was expecting closures to be something more "visible", a specific keyword, or a line where I could comment next to it. But if I understood it right, it's more like a concept, like a behavior of an inner function.
@lakshmi-dev
@lakshmi-dev 2 жыл бұрын
Very clear information. Thanks for sharing. Was reading many sources, but none were so clear and understandable. Can you please make a video on 'JS event bubbling'
@RodrigoButta
@RodrigoButta 4 жыл бұрын
So well explained! I'm fan of your videos since saturday! Thank you!! May be one thing missing in this one is to explain what happens when an outside variable gets modified inside a function (because i thinks it's not like php where you have to pass it as a reference and all that). Anyway, you rule. One of the best channels for learning! And the short video distribution is awesome to watch them at any time of the day.
@WebDevSimplified
@WebDevSimplified 4 жыл бұрын
Thank you so much. I have a video on pass by reference vs pass by value in JS that you can checkout that explains exactly this concept.
@dopetag
@dopetag Жыл бұрын
Nice! Some useful implementations and examples would be great for part 2 :)
@johnson554671
@johnson554671 2 жыл бұрын
Thx for such a great explanation!
@melissamoloney6312
@melissamoloney6312 4 жыл бұрын
That was very helpful! Thank you Kyle :)
@VishalSharma-fm6nr
@VishalSharma-fm6nr 2 жыл бұрын
This made closures so simple. Thanks!
@cristianbonomo5042
@cristianbonomo5042 4 жыл бұрын
Watched 1 minute of your video and understood closures. Thanks!
@iarchetype
@iarchetype 3 жыл бұрын
After 10 thousand videos, I was finally able to wrap my head around this passing of param straight to the inner function and not to the outer one, starting at: 3:48
@zerosecond9739
@zerosecond9739 2 жыл бұрын
great explanation ........ keep on uploading more concepts videos... You are gem to people who want to learn web development on own.....
@helloworld4475
@helloworld4475 4 жыл бұрын
Hi bro! Thanks for all the easy-understanding videos, and could you please make a video like ?
@milindkhadse556
@milindkhadse556 3 жыл бұрын
This is very helpful. Thank you
@lefosg
@lefosg Жыл бұрын
6:28 after watching the video once, repeat this for 5 times and you're good. Once more, thank you Kyle :)
@abdullatheefr2286
@abdullatheefr2286 Жыл бұрын
You are awsome. clear n crisp explaination.
@mikedelta658
@mikedelta658 Жыл бұрын
Finally, I am able to understand closure. Thanks!!!
@harrygill6169
@harrygill6169 3 жыл бұрын
The first example you gave is not due to closure but due to scope chaining. Closures come in play when the execution context has finished but the variables still stay around. In first example we still have global execution context. But thanks for video always learn something new everyday.
@ohiwantyoutobelieve
@ohiwantyoutobelieve Жыл бұрын
right. this is what i was thinking
@syedibadulhassan9307
@syedibadulhassan9307 4 жыл бұрын
Great explanation, thanks bud!
@_tu_shark
@_tu_shark 3 жыл бұрын
Nicely explained..! Thank You ..!
@Ravipenugonda
@Ravipenugonda 3 жыл бұрын
super and clear explanation. Thank you
@abdelrahmanmagdy3706
@abdelrahmanmagdy3706 2 жыл бұрын
Thank you so much Kyle for your outstanding explanation. I have a small comment, I think closures have different behaviors when you are using var and let right ?
@hargunbeersingh8918
@hargunbeersingh8918 2 жыл бұрын
I learn about closures on MDN, highly recommended!
@iiilililiklku
@iiilililiklku 4 жыл бұрын
awesome... I finally understood what closure is
@nickfan7867
@nickfan7867 3 жыл бұрын
Thanks for the explanation.
@kolyxix
@kolyxix 3 жыл бұрын
2:34." When most people think of closures they think of function inside a function." That has been the problem for me. Thanks for the clarification
@stith_pragya
@stith_pragya 5 ай бұрын
Thank You So Much for this wonderful video..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@naoko1927
@naoko1927 4 жыл бұрын
I do hit the like button every single video of you! btw, please make some videos about deployment, I really confused about some extra files like .editorconfig, Procfile, Yarnrc.yml, config files..etc. I'm looking forward to your videos. Fan from Vietnam !
@prathameshmali3888
@prathameshmali3888 3 жыл бұрын
Thanks man, really helpful.
@zarakikenpachi1773
@zarakikenpachi1773 11 ай бұрын
Closures allow functions to access variable values, declared in another (parent) function, even after the parent function has finished it's execution. In js, the variables within a function are accessible only within itself and only for as long as the function is executing. You can understand this, by creating a new variable and assigning the parent function to that variable. Then do 'console.dir(new variable). Try below example : const add = function(){ let num = 1; return function() { num++ return num; } } const addFunc = add() console.dir(addFunc) in the console > click on 'f anonymous()' > click on 'scopes' > click on 'closure' In the above example, the addFunc function is declared in the global scope. Hence, it should not be able to access the variable in the add function, because variables can only be accessed within the scope and and while the scope still exists (as long as the function is running). But because closure preserves the value of the variable even after the parent function (add) has finished execution, the other functions (addFunc) that are that are using the parent function are able to access the variable. Hope that helps. :)
@madhanmanick3455
@madhanmanick3455 4 жыл бұрын
Hey dude !!! Awesome and crisp explanation......
@pavloshchur3684
@pavloshchur3684 3 жыл бұрын
Great explanation!
@kareemmoro2935
@kareemmoro2935 2 жыл бұрын
That was a good explanation..nice
@dinarboyo3658
@dinarboyo3658 3 жыл бұрын
thanks it was really helpful
@billyjayamunga
@billyjayamunga 2 жыл бұрын
Great video! really simplifies the concept. What I still can't quite understand is how calling newFunction sets the inner variable. :/
@srkvocals
@srkvocals 2 жыл бұрын
very simple superb... thanks
@haraldcarlsten6238
@haraldcarlsten6238 2 жыл бұрын
Great video! Exactly! Why make it so complicated. An inner function has access to the outer functions variables and scope even after it has been called.
@dean6046
@dean6046 4 жыл бұрын
Thank you bro!
@dayibdesign1686
@dayibdesign1686 4 жыл бұрын
kyle resisted the terminator, now he changed the mission to simply web dev!!
@sankalparora9261
@sankalparora9261 3 жыл бұрын
Summed up great at the end.
@harishharidas8969
@harishharidas8969 3 жыл бұрын
Read atleast 10 articles to understand closures.. but this one saved me
@iHidden1
@iHidden1 4 жыл бұрын
Tomorrow i have an interview. I was thinking what i don't understand or know that well and i was searching right now to see what i can find. Closures were the first item on the list. Are you reading your subscribers minds? Thank you for this tutorial. Other items on list are call&apply and bind.
@ruslan2676
@ruslan2676 4 жыл бұрын
Good luck with the interview!
@BruceArmstrong09121997
@BruceArmstrong09121997 4 жыл бұрын
Loving this community
@asadbilal591
@asadbilal591 3 жыл бұрын
Man,why are you not getting views,so underrated..
@NirmalGhosh-yn7ov
@NirmalGhosh-yn7ov 2 жыл бұрын
Thanks bro.. I understood.
@KajalMajumder
@KajalMajumder 3 жыл бұрын
very nice explanation.
@saputrauta09
@saputrauta09 3 ай бұрын
Thankyou so much!
@Farrukhw
@Farrukhw 4 жыл бұрын
In other languages, the global variables are accessible everywhere. Variables in parent functions are accessible to inner functions. Variables in parent classes are accessible in the class functions as well as in child classes and their functions. (if set up as public or protected).
@filip23128
@filip23128 2 жыл бұрын
Thanks Kyle!
@Agustin-jo8mv
@Agustin-jo8mv 3 жыл бұрын
Wow I think this just clicked for me. Took me watching this a few times but the inner/outer variables seem to have helped.
@muhammadinaammunir6761
@muhammadinaammunir6761 Жыл бұрын
Today I had an interview and the interviewer asked me about closure. I did not give a proper answer and now I am here :).
@abdelrahman5094
@abdelrahman5094 3 жыл бұрын
best explanation ever
@heyyou494
@heyyou494 4 жыл бұрын
Thank you sensei
@JamesJon1187
@JamesJon1187 Жыл бұрын
Thank You!
@kemal_ozturk
@kemal_ozturk 4 жыл бұрын
The confused look in the thumbnail 🤣
@shivarammuthukumaraswamy7164
@shivarammuthukumaraswamy7164 3 жыл бұрын
Thanks a ton man
@naruto.boruto_edt
@naruto.boruto_edt Жыл бұрын
It's being 2y and this is still helpful thanks
@hughclarke8504
@hughclarke8504 3 жыл бұрын
Question, when and why would you use closures? I'm new to coding and I am learning closures now.
@jjjj5452
@jjjj5452 Жыл бұрын
I finally understand it after your explanation. You should do a udemy course
Learn Emmet In 15 Minutes - Double Your HTML Coding Speed
15:27
Web Dev Simplified
Рет қаралды 198 М.
JavaScript Closures Tutorial (Explained in depth)
19:03
ColorCode
Рет қаралды 60 М.
where is the ball to play this?😳⚽
00:13
LOL
Рет қаралды 14 МЛН
How To Choose Ramen Date Night 🍜
00:58
Jojo Sim
Рет қаралды 56 МЛН
How to open a can? 🤪 lifehack
00:25
Mr.Clabik - Friends
Рет қаралды 14 МЛН
Reference Vs Value In JavaScript
15:12
Web Dev Simplified
Рет қаралды 177 М.
Learn JSON in 10 Minutes
12:00
Web Dev Simplified
Рет қаралды 3,1 МЛН
Lexical scoping and Closure | chai aur #javascript
26:48
Chai aur Code
Рет қаралды 59 М.
Learn Debounce And Throttle In 16 Minutes
16:28
Web Dev Simplified
Рет қаралды 174 М.
Learn Regular Expressions In 20 Minutes
20:52
Web Dev Simplified
Рет қаралды 1,2 МЛН
Learn JavaScript Scoping In 10 Minutes
11:39
Web Dev Simplified
Рет қаралды 56 М.
Javascript interview | Closure
11:44
Hitesh Choudhary
Рет қаралды 33 М.
Learn JavaScript CLOSURES in 10 minutes! 🔒
10:58
Bro Code
Рет қаралды 10 М.
where is the ball to play this?😳⚽
00:13
LOL
Рет қаралды 14 МЛН