Javascript Closure Tutorial | Closures Explained

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 พ.ย. 2024

ความคิดเห็น • 404

  • @DaveGrayTeachesCode
    @DaveGrayTeachesCode  3 ปีที่แล้ว +78

    Closure is often confused with Lexical Scope. Lexical Scope is an important part of closure, but it is not closure by itself. This tutorial provides several examples that should allow you to distinguish between lexical scope and closure. This tutorial should also allow you to learn how to give examples of closure if you are asked during the interview process. It is important to understand scope when we discuss closure. If you need to learn more about scope, check out my scope tutorial here: th-cam.com/video/_E96W6ivHng/w-d-xo.html

    • @siya.abc123
      @siya.abc123 3 ปีที่แล้ว +2

      Thank you Dave ❤️

    • @YuRiSsS
      @YuRiSsS 3 ปีที่แล้ว +2

      Thank you for another great video Dave!

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  3 ปีที่แล้ว +1

      @@YuRiSsS you're welcome! 🙏

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว +1

      @Abhijit Desaithank you! That's a great request 💯 I like that idea.

    • @Krisler12
      @Krisler12 2 ปีที่แล้ว +1

      Hi!
      Supposing you have some secret JavaScript functions that you don't want anyone to know. Also, that code (functions) are very resource consuming and this is why they need to be client side and not server side so you can't write those functions in python or php.
      Also, obfuscation of JavaScript is not useful since it can be easily deobfuscated or seen by placing a breakpoint in the debugging tools in the browser.
      This been said, could you make, please, a tutorial teaching us the best way to protect your secret JavaScript functions without paying for third party services?
      Thank you so much in advance!

  • @ukaszpospiech3817
    @ukaszpospiech3817 3 ปีที่แล้ว +76

    Out of all the yt tutorials on closures I've watched today, this one's been by far the best. Clearly explained step-by-step with readable code as illustration. Calm voice, proper English pronunciation and slow pace of speaking matter particularly to beginners like me. Unlike most teacher-coders on yt, you're actually capable of teaching. Thank you, good job, I'm going to watch more of your tutorials.

  • @binadarabzand2701
    @binadarabzand2701 3 ปีที่แล้ว +52

    Thank you, Dave! An extremely complex concept was explained clearly. Finally, I understood what closure is and how to use it in my code. The IIFE was a cherry on top. Thank you again.

  • @BigSmoke-r9w
    @BigSmoke-r9w 8 หลายเดือนก่อน +1

    Dave grey taught me html and css! Now he's teaching me one of the javascript topics I find hard to understand! I love this guy, he's the best!!

  • @adventurer2395
    @adventurer2395 2 ปีที่แล้ว +13

    You have a laid back, natural, and clear way of explaining things. Much appreciated!

  • @aya2222
    @aya2222 3 ปีที่แล้ว +5

    I spent the whole day looking up videos to understand closure, and finally I could find the best one. Thank you so much.

  • @captainalpha4853
    @captainalpha4853 ปีที่แล้ว +5

    You explained closures in a way that even a primary school student will understand. You definitely deserve a sub. Thank You so much, Dave!

  • @SaketK11
    @SaketK11 10 หลายเดือนก่อน +1

    I has started learning web dev from your html videos ...last april .. thanx for teaching us and making begginer understand the relevant topics....

  • @jim_andrus6466
    @jim_andrus6466 2 ปีที่แล้ว

    I can't tell you how much I appreciate the pace of your speaking voice when explaining these concepts. So many of these youtube videos, the person is talking insanely fast, and I cannot keep up with all of it.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว

      I'm glad it helps! 💯 If anyone wants it faster, TH-cam lets them speed it up 😀

  • @khaledbenshible2657
    @khaledbenshible2657 ปีที่แล้ว +1

    I spent the whole day looking up videos to understand closure. finally i git it so Thank you so much for clearly and simply explaining

  • @jennisonb37
    @jennisonb37 ปีที่แล้ว +1

    I discovered you today, Dave and I am highly impressed with your teaching style. Unlike many other YT videos that teach all this stuff, I can actually understand you. (So many are hard to follow due to the thick accent of the instructor.) I'm looking forward to delving into other videos you have. Thank you!

  • @nirmalshah9947
    @nirmalshah9947 2 ปีที่แล้ว +2

    This is the first video I've come across, so far, that really goes deep into clearly explaining that simply having an inner function and logging its parents' variables isn't a closure but a lexical scope. Brilliant!

  • @m-ok-6379
    @m-ok-6379 ปีที่แล้ว +1

    I was writing closure example after example without really understanding the reason behind it and this video with credit example finally got the closure idea to click. Thank You.

  • @SC-bv6kx
    @SC-bv6kx 3 ปีที่แล้ว +5

    Just learned closure several days ago. This video definitely helped me review the concept. It also helped me clear up the confusion about lexical scope. Thanks Dave!

  • @MasPensador
    @MasPensador 9 หลายเดือนก่อน +1

    Finally I fully understood closure.... thank you and bless you

  • @dileepbc5901
    @dileepbc5901 ปีที่แล้ว

    this is an underrated tutorial, i had to got to the bottom of the page on the youtube to get this video. these explanations & examples are more than enough to understand what closure is.

  • @mertasilturk8400
    @mertasilturk8400 2 ปีที่แล้ว

    i watched too many closure videos but first time i understand clearly . thank you so much

  • @Albert_Hall
    @Albert_Hall 2 ปีที่แล้ว +1

    Great! Great! Great! Thank you very much! Very good explanation! Very good examples!

  • @ichiroutakashima4503
    @ichiroutakashima4503 ปีที่แล้ว

    The majority of videos made by other content educational creators across various websites talk like they're rappers, you sir are different. Things went much clearer after this video and made me realize that the majority of my confusion is because most of my searches were actually lexical scopes and not closures.
    I really am indebted to you.

  • @je3043
    @je3043 ปีที่แล้ว +1

    Just a quick thought.
    I think the "credits =- 1" line should be at the end of the function since we need to check credits' amount first to evaluate the possibility of playing. In the example we should be able to play 3 times and it only allows two plays.
    Everything aside, great video and I always find your explanations very helpful!

  • @stavroskefaleas6320
    @stavroskefaleas6320 4 หลายเดือนก่อน

    Best video tutorial on javascript closures on the whole internet.
    Period!

  • @joro353
    @joro353 หลายเดือนก่อน

    This is by far the best tutorial on closures I've watched. Thank you!

  • @hongzhonghu6364
    @hongzhonghu6364 2 ปีที่แล้ว

    The best and detailed "Closure" explanation on the internet

  • @erfanfarhadi
    @erfanfarhadi ปีที่แล้ว +1

    your last example was lovely , thank you

  • @toyosisalami6453
    @toyosisalami6453 2 ปีที่แล้ว

    Before coming across your video I had already watched three other ones that weren't explanatory enough for me. Thank you so much for this video; I got everything. Thank you.

  • @umairyatoo8490
    @umairyatoo8490 2 ปีที่แล้ว +4

    Thank you very much, Dave. You explained it in a very beautiful way possible. You know I had been struggling from this since long. There wasn't any such explanation on the internet! Thanks a lot.

  • @gerar10s
    @gerar10s 3 ปีที่แล้ว +3

    Great video! This explains a lot and you've got a new subscriber. It might be my lack of Javascript knowledge but I still don't quite understand why after the first time, every time the parent function is called, only the child function executes and not the WHOLE parent function. I'm going to watch it a few more times and see if it sinks in. Thanks for the video!

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  3 ปีที่แล้ว +6

      Glad it was helpful! This explanation may help... The parent function is only called once. It returns the "child" function. The variable is therefore set to the child function that is returned. Afterwards, using the variable, only the child function is called. The closure allows the child function to still access the variable that was created within the parent. What we're really referring to here is the lexical scope of the function that is returned, but using the parent and child terminology could help! 😀🚀

    • @ARandomPerson--
      @ARandomPerson-- 2 ปีที่แล้ว

      Wow that quick explanation really helped. Thx

  • @abdelbasset8280
    @abdelbasset8280 3 ปีที่แล้ว +1

    Thanks Dave you could help me to understand the most confusing concept in js, after I read a lot of articles and watched a ton of tutorials.

  • @arescatalin1407
    @arescatalin1407 3 ปีที่แล้ว +8

    I have smashed that like button and I hope that this playlist keeps growing as these concepts really make a difference. Thank you for sharing your knowledge with us

  • @connectyonas2149
    @connectyonas2149 2 ปีที่แล้ว

    I really found this explanation about lexical scope and closure the best one from TH-cam. Thank you.

  • @DLeRoux
    @DLeRoux 2 ปีที่แล้ว

    Dave, you need to seriously consider putting together a Javascript course with projects for Udemy. Your explanations are crystal clear. I have found myself doing course after course, stopping, and then trying to find someone else who would be able to explain the concept a bit clearer than the instructor. It wastes a lot of time. I have found myself coming here for the past three or so concepts, and they click straight away. So thank you.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว

      Thank you, Darryl! I do hope to put a premium course together sometime later this year. I am not sure if Udemy is where I will launch it though.

  • @wallacepei4529
    @wallacepei4529 3 ปีที่แล้ว +1

    Thank you for clarifying IIFE, it's syntax that has been tripping me up and now I understand what it's doing. Excellent demonstrations for closures.

  • @proactivex
    @proactivex 3 ปีที่แล้ว

    I watched maybe 10 videos on closure. Finally one person can explain it well, Dave Gray! You also crushed on that song white ladder.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  3 ปีที่แล้ว

      Ha! I do share my name with a few others, but I'm glad I could help with closures! 💯🚀

  • @wzup23
    @wzup23 6 หลายเดือนก่อน

    Thanks dude, it's so clear. I will definitely rewatch this over and over again until I am very familiar with the concept.

  • @georgesword2012
    @georgesword2012 ปีที่แล้ว +1

    Your tutorials are simply the best on TH-cam, thanks for your effort you put for creation high quality content for free ! ❤

  • @TusharBorawake
    @TusharBorawake 2 ปีที่แล้ว

    This is what closure is...I was just thinking lexical scope is closure. Superbly explained. Thanks alot.

  • @johnmcaulay4348
    @johnmcaulay4348 3 ปีที่แล้ว +2

    Thanks man, I've been struggling with this for days! This video finally made me understand closures (it gave me the aha! moment).
    I liked the way your video went beyond the cliche closure examples which weren't allowing me to actually understand practical uses for closures.
    Subscribed!

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  3 ปีที่แล้ว +2

      Thanks again, John - check out my tutorials on debounce, throttle, and memoize to see closures put to good use, too. 🚀

  • @mno7x
    @mno7x ปีที่แล้ว +1

    Thank you for your video! This is the most understandable explanation of closures I've seen and has solidified my understanding of the concept. Really appreciate the emphasis on what "lexical scope" is and how it's an integral part of -- but not the whole story with -- closures. Truly awesome! Thank you, good sir!

  • @stephaniezun
    @stephaniezun 2 ปีที่แล้ว +4

    Thank you so much for clearly and simply explaining this concept!

  • @shivashankar6043
    @shivashankar6043 2 ปีที่แล้ว +3

    Thanks Dave for this video, please continue the advanced js playlist. You explaining hard concepts easy to understand.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว +3

      You're welcome, Shiva! I do plan to do more 🚀🙏

  • @dopetag
    @dopetag ปีที่แล้ว

    The best teacher to explain advanced concepts of Javascript. Instant subscription!

  • @shadabs000
    @shadabs000 2 ปีที่แล้ว

    Finally , finally I could wrap my head around this concept. It's so basic and easy.

  • @alexandra.thegreat
    @alexandra.thegreat ปีที่แล้ว +1

    Excellent explanation, thank very much for this video!

  • @tanzimchowdhury320
    @tanzimchowdhury320 ปีที่แล้ว +1

    Thank you Dave, you just got a new subscriber

  • @RandyHawkinsMD
    @RandyHawkinsMD ปีที่แล้ว

    Clear, simple, concise, well spoken and very useful. I followed along with your code in my IDE (Velo) with no difficulty. Thank you.

  • @MB-hn5qk
    @MB-hn5qk 7 หลายเดือนก่อน

    Probably the best Closures video I have ever watched. Thanks!!

  • @gamewidge
    @gamewidge หลายเดือนก่อน

    Hi DaveGray,
    I’m a student and have completed learning the MERN stack from your channel. I would love to see a course focused on building microservices with serverless architecture. It would be great if you could cover topics like designing, deploying, and scaling microservices using platforms such as AWS Lambda please.

  • @szangyi24
    @szangyi24 ปีที่แล้ว

    Dave, thank you so much for this tutorial. I finally got my "Gotchaa!" moment about closures. I watched several explanations before but this really helped me to understand. Lifesaver :)

  • @yogeshkhatri4555
    @yogeshkhatri4555 ปีที่แล้ว

    People misunderstand js a lot. People like u make it relevant for us js peeps. Keep going!

  • @mi5956
    @mi5956 2 ปีที่แล้ว

    Thank you for clear examples, I finally understood. I looked at other youtubers and it was confusing.

  • @estherokiki5894
    @estherokiki5894 8 หลายเดือนก่อน

    Thanks for all you do, Dave. Your videos are the best. I've watched several videos on closures, but none of them explained it better than you did. I finally understand closures!

  • @kostiantynkarzhanov9216
    @kostiantynkarzhanov9216 ปีที่แล้ว +2

    Dave, thank you so much for your tutorials! All of them are very well structured and easy to understand. I really appreciate all the work you do. Thanks again. Awesome channel! 💛

  • @srinivasaraoyp3640
    @srinivasaraoyp3640 2 ปีที่แล้ว

    In the beginning, I found it difficult to understand the topic "closures" it is so confusing. After watching your video demo, I understood it. Thank you very much Dave.

  • @youmnaification
    @youmnaification 2 ปีที่แล้ว

    This is by far the best explanation of JS closure. Thank you Dave. I'm glad I came across your channel.

  • @kumarisuman4565
    @kumarisuman4565 2 ปีที่แล้ว +1

    This was a great video. I was struggling with closures for some days but now I feel like i understand it now. THANKS Again it was a great video.

  • @oscargm1979
    @oscargm1979 2 หลายเดือนก่อน

    Perfectly perfectly and perfectly explained. Congrats

  • @gopishivakrishna9707
    @gopishivakrishna9707 2 ปีที่แล้ว +1

    Not bored at all. Nicely presented. :)

  • @bethcarretta5715
    @bethcarretta5715 2 ปีที่แล้ว +1

    I wish I had stumbled across your video sooner! I've been trying to wrap my brain around closures for so long. Super helpful! You got a new subscriber!

  • @NadaAlSharqawy
    @NadaAlSharqawy 4 หลายเดือนก่อน

    This is the only explanation that made me understand it, thank you

  • @hackmeornot2196
    @hackmeornot2196 2 ปีที่แล้ว

    this man, helps me to understand closure quickly and easily!

  • @sugarfairy1248
    @sugarfairy1248 8 หลายเดือนก่อน

    well chosen examples demonstring the unique characteristics of closure. Finally undertand it now!

  • @imperativeemarketing5546
    @imperativeemarketing5546 2 ปีที่แล้ว

    I "Thumbs Upped" your video. I've been studying JS recently for a few months, and to my surprise everything is getting easier to understand, like I'm on the steeper part of the learning curve. I've been getting a clearer picture of what closures are, but your explanation helped even more to demystify closures to me. You have a very sensible, logically explanatory and intelligent way of explaining closures. I'll be checking out your other videos because I like your teaching style. Who knows, maybe I'll buy you a cup of coffee :).

  • @zm690
    @zm690 2 ปีที่แล้ว

    This is the best closure explanation for me... Great work.

  • @kenluong1838
    @kenluong1838 3 ปีที่แล้ว

    Hello Dave. Your contents are tremendously helpful to me as a self-directed learner of frontend dev. You've got yourself a new subscriber :). Your explanation of JS closure is crystal clear but I still have a burning question which I hope your could provide clarification. I am tempted to say the reason that nested function still have access to outer function's variable, even after outer function has returned, is that garbage collection can't destroy these variable. They are still referenced by a nested function. In other words, these variables are still present even though the scope in which they were created is not. Thanks again for your time and efforts!

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  3 ปีที่แล้ว

      You're welcome! Good comment, too. I believe you can say the lexical scope of the function still exists because the function exists. 🚀💯

    • @kenluong5081
      @kenluong5081 3 ปีที่แล้ว

      Concisely put!

  • @tasmimtithe3793
    @tasmimtithe3793 ปีที่แล้ว

    Nice video 🎉. I think the definition would be when a function returns another function that is defined inside the function is a closure function!

  • @renras1057
    @renras1057 2 ปีที่แล้ว

    Best video about closure so far. You are a very good teacher.

  • @kalyann3426
    @kalyann3426 ปีที่แล้ว

    It’s so refreshing to listen,well explained.Thank you Dave

  • @abhinavtiwari89
    @abhinavtiwari89 7 หลายเดือนก่อน

    This is the best tutorial on Closures.
    Thank You!

  • @GeorgiosAmsterdam
    @GeorgiosAmsterdam ปีที่แล้ว

    finally found a video about closures that makes sense. Thanks

  • @harvard004
    @harvard004 2 ปีที่แล้ว

    12:45 The important highlight he mentions.
    IIFE code happens once. The anonymous function inside it only get's returned ONE time (to privateCounter). After that, you can look at privateCounter as:
    privateCounter = () => { count +=1; console.log(count) } //but with it's own access to private variable "count".
    -I'm guessing you can say it has enClosed it's scope over the function and it's declared variables. 5:45

  • @AR7editing
    @AR7editing ปีที่แล้ว

    Thank you, I really thought that I know closures, but I was completely wrong, nice video

  • @SuperDarmino
    @SuperDarmino ปีที่แล้ว

    Replayed this video several times. I think i now get it finally 😂. W3 definition makes alot of sense now.

  • @leoMC4384
    @leoMC4384 2 ปีที่แล้ว

    I've been programming on JavaScript for over 2 years, I hope I'll feel ready to apply for a job soon, even being old to start, I'm 36. Until now, if I was asked about Closure I replied about lexical scope. The IIFE explanation was interesting although I knew it already, because I used them on my API. I do full stack with MERN. Anyway, great video, very clear explanation. 👏👏👏

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว +1

      Thank you, Leonardo 🙏 You're never too old to do what you love - keep making progress! 💯

  • @pharmakidsarl8449
    @pharmakidsarl8449 5 หลายเดือนก่อน

    One of the best video on closure

  • @antonchigurh4125
    @antonchigurh4125 2 ปีที่แล้ว

    This was a masterclass on how to explain abstract concepts with easy-to-follow, real-world examples. Thank you. Subbed!

  • @a5tr00
    @a5tr00 ปีที่แล้ว

    Finally I understood JS closures!
    Subscribed!

  • @mohamadybr
    @mohamadybr 2 ปีที่แล้ว

    This tutorial is so well structured it simplified the closure concept in a brilliant way!! Thanks a lot

  • @practicalandplannedhomeschool
    @practicalandplannedhomeschool 2 ปีที่แล้ว

    This video was an instant subscribe. It was well thought out and clear, and I now understand two concepts much better than ever before. Closure kind of blew my mind. Thank you so much for sharing your knowledge!

  • @ToddPressleyy
    @ToddPressleyy 2 ปีที่แล้ว

    Really loved the ball field analogy! At times I’ve struggled to explain the concept of closures to teammates. this will help greatly… thanks for the great content!

  • @seephor
    @seephor 2 ปีที่แล้ว

    Very good explanation. I think what I really wanted to see by the use of closures was the outer function returning an object consisting of functions. This gives you the ability to isolate your data similar to how classes work.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว

      Thank you! You describe a nice advanced example! 💯🚀

  • @alimohammad-lg8sw
    @alimohammad-lg8sw ปีที่แล้ว

    Thank you so muchhhh
    The best closure explanation ever

  • @joynal_dev
    @joynal_dev 3 ปีที่แล้ว +1

    Your videos are absolute gems 💎. Keep up the good work.

  • @mikegogl
    @mikegogl ปีที่แล้ว

    How did I now know about this channel before. Thank you.

  • @alexmassy
    @alexmassy 2 ปีที่แล้ว

    Excellent ! I was struggling to understand dynamic between Closure and Lexical Scope.

  • @alexlions8633
    @alexlions8633 ปีที่แล้ว +1

    Good explanation, thank you sir :)

  • @eleah2665
    @eleah2665 10 หลายเดือนก่อน

    I just reviewed this. You could do a related one on Crockford objects - avoiding classes.

  • @GrahamO-g5g
    @GrahamO-g5g 8 หลายเดือนก่อน

    Best explaination on the web. Great job sir! Thank you!

  • @basharhamdan8055
    @basharhamdan8055 2 ปีที่แล้ว

    Thanks had problems with understanding Closure... wish my prof. could do as good of a job explaining thing like you do

  • @1050cc
    @1050cc 7 หลายเดือนก่อน

    Clean explanation, thank you ! Very powerful but... miserable to debug !??

  • @ibnulferdous
    @ibnulferdous ปีที่แล้ว

    You made closure clear to me! Thank you so much!

  • @chetanjain4616
    @chetanjain4616 2 ปีที่แล้ว

    just found out about this series! Loving them

  • @kaankara1101
    @kaankara1101 2 ปีที่แล้ว

    Good Job Dave people like you who make instructional videos are milestones for your knowledge

  • @InnaKasyan-f8h
    @InnaKasyan-f8h 2 ปีที่แล้ว

    Thank you ! The explanation is extremely clear for beginners.

  • @maxralph01
    @maxralph01 ปีที่แล้ว

    Awesome. Pretty common concept coming from Laravel, PHP (we call it "a function called from another function"). That made me familiar to the concept beforehand. But I was lost on the application in JavaScript.
    Amazing, top-notch explanation, Dave!
    Now, I can use it in JS.

  • @jennisonb37
    @jennisonb37 2 ปีที่แล้ว

    Dave, this was really excellently explained. It finally clicked for me. However, like most videos on this subject, I feel like a critical piece of information is missing. The why? So a closure allows me to hide (encapsulate) a function's local variable. Why would we want to do that?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  2 ปีที่แล้ว

      Thank you! Closure is used in many places once you recognize it. For example, it is used all the time in React. For Vanilla JS, here are a couple of tutorials that use it: 1) Debounce functions: th-cam.com/video/yBFHwJgqLD4/w-d-xo.html and 2) Memoization: th-cam.com/video/TWUV_LRVX24/w-d-xo.html

  • @grmensz
    @grmensz ปีที่แล้ว

    Thank you for an excellent learning experience! By far the best explanation of what initially appears to be a difficult concept. Subscribing so that I can actually learn to understand things from an excellent teacher!

  • @300deQI
    @300deQI 2 ปีที่แล้ว

    dang, that was the best closure tutorial that I've ever seen

  • @youssefkarfouh3084
    @youssefkarfouh3084 ปีที่แล้ว

    Thank you so much , for this explanation i was so confused about closure

  • @ExtraTurtle
    @ExtraTurtle 8 หลายเดือนก่อน

    I'd be happy to also see some closure pitfalls examples. closures are not that useful to know without pitfalls in my opinion. It's just something that you expect to work. however pitfalls show that sometimes closures are against you!

  • @romanr5962
    @romanr5962 2 ปีที่แล้ว

    Brilliant explanation IIFE , my friend.Thanks