Javascript Closure Tutorial | Closures Explained

Sdílet
Vložit
  • čas přidán 27. 06. 2024
  • Web Dev Roadmap for Beginners (Free!): bit.ly/DaveGrayWebDevRoadmap
    In this Javascript Closure Tutorial you will learn the concept of closure through multiple examples. Closures explained in this way may be easier to understand. Closure is often confused with lexical scope which is an important part of closure, but it is not closure itself.
    Subscribe ➜ bit.ly/3nGHmNn
    JavaScript for Beginners: Full Course (8 hours): • JavaScript Full Course...
    Javascript Scope for Beginners: • var let const in Javas...
    Javascript Closure Tutorial | Closures Explained
    (0:00) Intro
    (0:13) Example 1: Lexical Scope is not Closure / Closure Basics
    (9:55) Example 2: Closure with an IIFE
    (13:46) Example 3: Closure with an IIFE and a parameter
    ✅ Follow Me:
    Twitter: / yesdavidgray
    LinkedIn: / davidagray
    Blog: yesdavidgray.com
    Reddit: / daveoneleven
    Was this tutorial about Javascript Closures helpful? If so, please share. Let me know your thoughts in the comments.
    #javascript #closure #tutorial
  • Věda a technologie

Komentáře • 396

  • @DaveGrayTeachesCode
    @DaveGrayTeachesCode  Před 3 lety +71

    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: czcams.com/video/_E96W6ivHng/video.html

    • @buddy.abc123
      @buddy.abc123 Před 3 lety +2

      Thank you Dave ❤️

    • @YuRiSsS
      @YuRiSsS Před 2 lety +2

      Thank you for another great video Dave!

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety +1

      @@YuRiSsS you're welcome! 🙏

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety +1

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

    • @Krisler12
      @Krisler12 Před 2 lety +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 Před 2 lety +73

    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 Před 2 lety +50

    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.

  • @adventurer2395
    @adventurer2395 Před 2 lety +11

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

  • @nirmalshah9947
    @nirmalshah9947 Před rokem +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!

  • @jennisonb37
    @jennisonb37 Před 9 měsíci +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!

  • @aya2222
    @aya2222 Před 3 lety +5

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

  • @khaledbenshible2657
    @khaledbenshible2657 Před 11 měsíci +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

  • @captainalpha4853
    @captainalpha4853 Před rokem +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!

  • @MasPensador
    @MasPensador Před 4 měsíci +1

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

  • @user-kn4oi9oh4i
    @user-kn4oi9oh4i Před 4 měsíci +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!!

  • @jim_andrus6466
    @jim_andrus6466 Před rokem

    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  Před rokem

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

  • @tojifushigoro3134
    @tojifushigoro3134 Před 5 měsíci +1

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

  • @SC-bv6kx
    @SC-bv6kx Před 3 lety +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!

  • @arescatalin1407
    @arescatalin1407 Před 2 lety +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

  • @umairyatoo8490
    @umairyatoo8490 Před 2 lety +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.

  • @kostiantynkarzhanov9216
    @kostiantynkarzhanov9216 Před rokem +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! 💛

  • @mno7x
    @mno7x Před 11 měsíci +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!

  • @georgesword2012
    @georgesword2012 Před rokem +1

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

  • @johnmcaulay4348
    @johnmcaulay4348 Před 2 lety +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  Před 2 lety +2

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

  • @stavroskefaleas6320
    @stavroskefaleas6320 Před 11 hodinami

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

  • @m-ok-6379
    @m-ok-6379 Před rokem +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.

  • @bluevojka
    @bluevojka Před 3 měsíci

    This was Amazing Dave!!

  • @bethcarretta5715
    @bethcarretta5715 Před 2 lety +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!

  • @tanzimchowdhury320
    @tanzimchowdhury320 Před 7 měsíci +1

    Thank you Dave, you just got a new subscriber

  • @stephaniezun
    @stephaniezun Před 2 lety +4

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

  • @sugarfairy1248
    @sugarfairy1248 Před 3 měsíci

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

  • @proactivex
    @proactivex Před 3 lety

    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  Před 3 lety

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

  • @wzup23
    @wzup23 Před měsícem

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

  • @ichiroutakashima4503
    @ichiroutakashima4503 Před rokem

    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.

  • @ikurbano
    @ikurbano Před 2 lety

    thank you, Dave! as always, easy to understand, yet consise and to the point.

  • @estherokiki5894
    @estherokiki5894 Před 3 měsíci

    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!

  • @kalyann3426
    @kalyann3426 Před rokem

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

  • @MB-hn5qk
    @MB-hn5qk Před 2 měsíci

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

  • @alimohammad-lg8sw
    @alimohammad-lg8sw Před 8 měsíci

    Thank you so muchhhh
    The best closure explanation ever

  • @chetanjain4616
    @chetanjain4616 Před 2 lety

    just found out about this series! Loving them

  • @mertasilturk8400
    @mertasilturk8400 Před rokem

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

  • @youmnaification
    @youmnaification Před rokem

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

  • @pharmakidsarl8449
    @pharmakidsarl8449 Před 10 dny

    One of the best video on closure

  • @hackmeornot2196
    @hackmeornot2196 Před 2 lety

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

  • @vuleticvladimir256
    @vuleticvladimir256 Před 2 lety

    Good job Dave, thank you so much.

  • @shadabs000
    @shadabs000 Před 2 lety

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

  • @FindingFocus
    @FindingFocus Před měsícem

    thank you for wonderful examples! I didn't know this was possible

  • @connectyonas2149
    @connectyonas2149 Před 2 lety

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

  • @abdelbasset8280
    @abdelbasset8280 Před 2 lety +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.

  • @jiweihe3413
    @jiweihe3413 Před 2 lety

    Thank you! Finally understood this important concept.

  • @slpn1
    @slpn1 Před rokem

    Clearest explanation of what closures are I've found. Great stuff.

  • @dileepbc5901
    @dileepbc5901 Před rokem

    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.

  • @GeorgiosAmsterdam
    @GeorgiosAmsterdam Před rokem

    finally found a video about closures that makes sense. Thanks

  • @a5tr00
    @a5tr00 Před rokem

    Finally I understood JS closures!
    Subscribed!

  • @ibnulferdous2349
    @ibnulferdous2349 Před rokem

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

  • @seragilulfo137
    @seragilulfo137 Před 2 měsíci

    this helped me a lot,thank you

  • @alexandra.thegreat
    @alexandra.thegreat Před 10 měsíci +1

    Excellent explanation, thank very much for this video!

  • @onlyonwed526
    @onlyonwed526 Před 4 měsíci

    Thanks. You re the best explanator :D

  • @toyosisalami6453
    @toyosisalami6453 Před 2 lety

    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.

  • @carsugarmodels
    @carsugarmodels Před 2 lety

    Awesome explanation! Subscribed and digging other videos! Thanks man!

  • @atbt230
    @atbt230 Před 3 lety

    Thank you, Dave. I have enjoyed your Closuer explanation.

  • @BabaYaga89535
    @BabaYaga89535 Před 2 lety

    Thank you so much. I finally understand closures

  • @joosepserva763
    @joosepserva763 Před rokem

    Thanks, Dave! You are really good at explaining! 😊

  • @manochpriyankara6379
    @manochpriyankara6379 Před 9 měsíci

    wow...Great explanation...Thank you, Dave...

  • @shivashankar6043
    @shivashankar6043 Před 2 lety +3

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

  • @antonchigurh4125
    @antonchigurh4125 Před rokem

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

  • @user-by8xy2ne5w
    @user-by8xy2ne5w Před 3 měsíci

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

  • @hongzhonghu6364
    @hongzhonghu6364 Před 2 lety

    The best and detailed "Closure" explanation on the internet

  • @user-lq1mm2tn5v
    @user-lq1mm2tn5v Před rokem

    Thank you ! The explanation is extremely clear for beginners.

  • @mohamadybr
    @mohamadybr Před 2 lety

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

  • @abhinavtiwari89
    @abhinavtiwari89 Před 2 měsíci

    This is the best tutorial on Closures.
    Thank You!

  • @imperativeemarketing5546

    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 :).

  • @joynal_dev
    @joynal_dev Před 3 lety +1

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

  • @je3043
    @je3043 Před 10 měsíci +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!

  • @mi5956
    @mi5956 Před 2 lety

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

  • @erict8600
    @erict8600 Před rokem

    thank you so much, this helped tremedously! subbed!

  • @practicalandplannedhomeschool

    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!

  • @wallacepei4529
    @wallacepei4529 Před 2 lety +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.

  • @Albert_Hall
    @Albert_Hall Před rokem +1

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

  • @sergey.gabrielyan1998
    @sergey.gabrielyan1998 Před 7 měsíci

    It was very interesting, thank you so much

  • @RD-jr8nv
    @RD-jr8nv Před rokem

    Great videos man. Loving your content. Love the axe behind you too, RIP Eddie!

  • @joannbarney7210
    @joannbarney7210 Před 2 lety

    Excellent explanation of closure. Thank you!

  • @DLeRoux
    @DLeRoux Před 2 lety

    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  Před 2 lety

      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.

  • @RandyHawkinsMD
    @RandyHawkinsMD Před rokem

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

  • @ToddPressleyy
    @ToddPressleyy Před rokem

    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!

  • @mohammedamaar2156
    @mohammedamaar2156 Před rokem

    thank you very much for putting effort in making these tutorials !!!

  • @lanceturbes2518
    @lanceturbes2518 Před 2 lety

    Great video! Cleared up a lot of confusion.

  • @gopishivakrishna9707
    @gopishivakrishna9707 Před 2 lety +1

    Not bored at all. Nicely presented. :)

  • @mariacamilamontealegremerc5892

    Awesome, thank you!

  • @zm690
    @zm690 Před 2 lety

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

  • @soumadip_skyy_banerjee
    @soumadip_skyy_banerjee Před rokem +1

    Thank you Mr.Gray 🤍

  • @lulusaikou221
    @lulusaikou221 Před rokem

    Really helpful! Thank you!

  • @Mac-vn5rf
    @Mac-vn5rf Před 8 měsíci

    great explanation!

  • @dopetag
    @dopetag Před rokem

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

  • @samahgad241
    @samahgad241 Před rokem

    I love your explanation so much, thanks from Egypt.

  • @erfanfarhadi
    @erfanfarhadi Před rokem +1

    your last example was lovely , thank you

  • @youssefkarfouh3084
    @youssefkarfouh3084 Před rokem

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

  • @rajgopal2513
    @rajgopal2513 Před 2 lety

    This is really great explanation! Thank-you Mr. Dave

  • @kbzcomuy
    @kbzcomuy Před rokem

    muy bueno y super claro, gracias Dave. ;)

  • @DMG5588
    @DMG5588 Před rokem

    I finally understood it. Thank you 😊

  • @romanr5962
    @romanr5962 Před 2 lety

    Brilliant explanation IIFE , my friend.Thanks

  • @renras1057
    @renras1057 Před 2 lety

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

  • @RafaelSFTech
    @RafaelSFTech Před rokem

    Super usefull examples, congratz man!

  • @yogeshkhatri4555
    @yogeshkhatri4555 Před rokem

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