ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก

Learn Closures In 13 Minutes

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 ส.ค. 2024
  • JavaScript Simplified Course: javascriptsimp...
    FREE JS Array Methods Cheat Sheet: webdevsimplifi...
    FREE JS DOM Traversal Cheat Sheet: webdevsimplifi...
    FREE JS DOM Manipulation Cheat Sheet: webdevsimplifi...
    Closures are one of the core features of JavaScript that every developer needs to understand. You not only need to know what they are and how they work, but you also need to have a deep understanding of various niche behaviors of closures since many jobs will ask you complex interview questions on closures. In this video I will cover what closures are, how they work, and multiple example interview questions so you can be prepared for whatever gets thrown at you.
    📚 Materials/References:
    JavaScript Simplified Course: javascriptsimp...
    FREE JS Array Methods Cheat Sheet: webdevsimplifi...
    FREE JS DOM Traversal Cheat Sheet: webdevsimplifi...
    FREE JS DOM Manipulation Cheat Sheet: webdevsimplifi...
    Scoping Video: • If You Cannot Name All...
    Scoping Article: blog.webdevsim...
    Garbage Collection Video: • Only The Best Develope...
    Strict Mode Video: • JavaScript Was So Bad ...
    ES6 Modules Video: • JavaScript ES6 Modules
    ES6 Modules Article: blog.webdevsim...
    Arrow Functions Video: • JavaScript ES6 Arrow F...
    Arrow Functions Article: blog.webdevsim...
    Let vs Var vs Const Video: • Differences Between Va...
    Let vs Var vs Const Article: blog.webdevsim...
    🌎 Find Me Here:
    My Blog: blog.webdevsim...
    My Courses: courses.webdev...
    Patreon: / webdevsimplified
    Twitter: / devsimplified
    Discord: / discord
    GitHub: github.com/Web...
    CodePen: codepen.io/Web...
    ⏱️ Timestamps:
    00:00 - Introduction
    00:50 - What Is A Closure
    05:40 - Why Are Closures Important
    08:36 - Closure Interview Questions
    #Closures #WDS #JavaScript

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

  • @usmanquickcode
    @usmanquickcode หลายเดือนก่อน +56

    I just gave my interview and in this interview they asked me about closures. which ofcourse i have no idea. I have used it but no idea what is called. and i open my youtube and boom you are there. really like the simple explaination.

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

      Those know-it-all interviewers always play with these gotcha questions which are not even practical anymore

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

      @@anothermouth7077closures are pretty important tho

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

      Such useless interview questions. They really need to ask about the job instead of stupid shit that you're not gonna use 95% of the time

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

      ​@thecoolnewsguy you will use closures constantly though, and terminology is important for proper communication...

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

      @@jessecalato4677 I know. Yes closures are used extensively but asking about the var gotcha in a loop is just stupid like who's gonna use var in 2024 for a new code?

  • @joesharp3580
    @joesharp3580 22 ชั่วโมงที่ผ่านมา

    Just incredible video as always. Somehow you always get to the absolute core of a concept and then demonstrate it beautifully

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

    I use one type closure all the time, combined with promises, when I have to execute a function on an array of data asynchronously, all you have to do is return a promise with a function, inside create a couter at 0, create the function thats going to process an entry, before return, incremente the counter, and excecute the inner function again, when its done, resolve the promise.

  • @mewansynniang1902
    @mewansynniang1902 27 วันที่ผ่านมา +2

    Beautifully explained

  • @xzex2609
    @xzex2609 หลายเดือนก่อน +14

    React vue and angular use closures all the time , all functional components are closures

    • @maciejzettt
      @maciejzettt หลายเดือนก่อน +3

      As well as state management is

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

      And react hooks to 😅

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

      @@Regeneration1996 of course in the end a hook will return some functions as objects and states too , when I was said react I mean most of it uses closures

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

    Thanks a lot, I know and remember how closure works in javascript, but I never knew about something tricks.

  • @mhadi-dev
    @mhadi-dev หลายเดือนก่อน +4

    Best used in Factory design pattern.

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

    dude be selling 10 other videos within 1 video

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

    That for loop let var closure interview question is just brutal

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

    Thank you! Amazing content!

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

    I had doubts for this last example, so I asked gpt to explain it to me deeply. It errored the first few times but at last gave the correct answer which claude also verified.
    Yes, for each iteration of the loop, a new scope is created, and within that scope, a new binding for the variable i is created. Here’s a detailed breakdown:
    First Iteration (i = 0):
    A new scope is created.
    A new binding for i is created and initialized to 0.
    The setTimeout callback captures this binding, so it logs 0.
    Second Iteration (i = 1):
    A new scope is created.
    A new binding for i is created and initialized to 1.
    The setTimeout callback captures this binding, so it logs 1.
    Third Iteration (i = 2):
    A new scope is created.
    A new binding for i is created and initialized to 2.
    The setTimeout callback captures this binding, so it logs 2.
    In each iteration, the let declaration ensures that i is re-declared and re-initialized within the new scope. This results in three separate bindings for i, each one unique to its iteration.

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

    Great quick watch

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

    Printing 3 times 3 has more to do with the event loop than closures

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

    So, if I can sum up and get confirmation: a closure is where an outer function or scope is not garbage collected because there is an inner function that relies on the outer function or scope's variables... is this correct?

  • @saurabhshrivastava5383
    @saurabhshrivastava5383 25 วันที่ผ่านมา

    Hello Kayal, In your second example for closuers "function elementCreator(element)", if we directly return document.createElement(element) instead returning a function then what difference will it make, I think direct return will be better, what you suggest.

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

    This is the best explanation of closures I've seen (including your own previous explanations).

  • @AlejandroPoleo18
    @AlejandroPoleo18 16 วันที่ผ่านมา

    ❤ love it

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

    Excellent and really clear approch and explantion

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

    Great video as usual. But Kyle, your 1x Playback speed feels like 1.25x bro. 🙂
    Interviewers also ask that how you will print 0,1,2 when using var i=0; . We have to use the concept of closure here.

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

    it would be good if you cover conditional scope or conditional closures

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

    I think the explanation for var, let with for loop is a bit wrong. 🤔

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

      I ran the code with the var and got 0,1,2 🤔

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

      Oh its because i had an error few lines before this I guess its js weird engine

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

    Great video!

  • @QuanTran-wt4yt
    @QuanTran-wt4yt หลายเดือนก่อน

    Can you make a video talking about message queue?

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

    Let's always do alot of good ❤

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

    Does it keep the dependant variable and delete the old ones?
    E.x. when you added age = 30, will age = 29 be kept still by javascript?

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

      No because it's the same memory spot.
      In his example, when he does age = 30, it overwrites the value 29 in variable 'age', instead of creating a new variable.

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

    What is the purpose of returning function which returns element and not returning element with the first function?

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

    Thank you for this video and the job you do, but what is a JavaScript Developer? The only thing i could find is "JS developer" - in react, angular, vue and stuff
    Is there atually a job for JS developer in JS?

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

    Thanks

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

    In fact the concept of react custom hooks relies on the notion of closure
    When you're building a react custom hook, you're actually using closure.

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

    Dear, could you please explain what is JavaScript reference memory?

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

    Pascal lets you do this as a well, never used that feature of pascal. But was forced to Learn it

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

    Hii brother...
    You explained let and var concept in for loop.
    But still I'm confused.
    Can you explain in some other way.

  • @Kingreey
    @Kingreey 21 วันที่ผ่านมา

    Excelent

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

    Sir please explain with the help of draw and after that code

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

    I honestly thought that it didn't call function too until it said return function to and since the variable was already age 30, that is why it started with 30. What if you called the function first?

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

    JavaScript Closures, bautifully explained. Thanks, Kyle
    {2024-07-09}

  • @green-coder-clj
    @green-coder-clj หลายเดือนก่อน

    I misread the title as "Learn Clojure in 13 minutes" 😅
    Clojure & Clojurescript indeed help landing a job for the FE.

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

    I was just wondering, is it possible to see the what structures&code JavaScript actually creates? Don't know how to call it.
    In C#, you can see the different stages ( C#, IL, ASM ) and that makes perfectly clear how closures technically work.

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

      JavaScript doesn't create code it gets JIT compiled into machine code directly by the engine. Read about V8 and how it works

  • @MichaelKire
    @MichaelKire หลายเดือนก่อน +13

    Fun fact. Classes in JS is actually just syntactic sugar for a closure with a special constructor/prototype function inside

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

      While watching the video, this came to my mind... It's nice seeing that someone else thought of the same thing

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

      A class is syntactic sugar for a function, not necessarily a closure. A function becomes a closure when capturing variables from the parent scope, not because it’s a class.

  • @alienews0
    @alienews0 29 วันที่ผ่านมา

    5:00 nope u didn't called your function on that line, u declared it ; u called it on the last line : return func2

  • @DanielBrownsan
    @DanielBrownsan 27 วันที่ผ่านมา

    I genuinely thought I had the playback speed set to 150-200% for the first 3 minutes of the video.

  • @user-ik7rp8qz5g
    @user-ik7rp8qz5g หลายเดือนก่อน

    What you describe is just oop, but with extra steps

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

    Unrelated but why are you running VS Code as Admin? 🤔

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

    If you have a global function that captures a variable from the outer (global) scope, then this function is a closure, because a closure is a function that captures variables from the parent scope.
    Even if you are calling console.log, because console is a variable in the global scope, you are capturing a variable and therefore your function is a closure.
    The only functions in JavaScript that are not closures are pure functions.
    Please do some research before making a 13min video about a topic.

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

      Really makes me wanna buy his “course” xd

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

    Closure to scoping is like 0/1 to binary -- closure is how scoping is implemented in JS, while 0/1 is how binary is represented typically.

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

    Please create Next js e-commerce project catalog with multiple checkboxes filtering, sorting, pagination, search. More e-commerce realistic. Thank you.

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

    Do you know how to do that integer story with a public bank website inside a private login inside page two with a currency and interger value where I need to insert a legal agreement account balance update from 0 => R 1 000 000 Zar ? It’s a public https web address?! The javascript wipes the direct insert after page refresh how do I make the interger value const? Do you have team viewer and want to try and or show me?

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

      Hate to break it to you, but absolutely no one is going to help you with manipulating a bank website. Further, what you're seeing isn't just javascript wiping the value. It's retrieving the value from the bank's servers, which you cannot modify no matter how much you manipulate the page. Gotta study up a lot more and stop asking for advice on malicious activities on TH-cam. Also, on the off chance you do manage to do something malicious, you will almost certainly get caught.

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

      @@JacobZigenis my laptops storage is full and not booting at the moment so I cannot screenshare. I don't see how it's my problem the external and Internal banking staff cannot manually show me howto insert credits into bank tables in cobol or whatever the language is from the email pseudocode to the browser web security layers in the osi model so that I may make debits to pay attorney legal fees for bank account topups. I think it's worth a trial investigation ie look, screenshare, record discuss, etc and a look into the file and code framework on the Javascript and in the console just for a deeper explination, because I just know sql server, table updates, hidden servers, html, Javascript and css. The actual coding language and topology to see exactly where the update changes are made. Worse case the system gets reported and then gets fixed. I don't think it's too complicated less than 1000 lines of code plus minus with like 400 lines on my screen at once with like 30 lines each side of the zero balance variable.

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

    first comment after first comment of arunkaiser😂😂

  • @Psai4456
    @Psai4456 วันที่ผ่านมา

    one guy asked me in interview i told the wrong answer

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

    So they're called closures, well I'll be dammed. I've been calling them higher order functions all this time.

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

      They are kinda the same thing. Higher order functions return a function and usually, the function that is returned has access to variables inside the original function which is what closures is. So yeah different concepts but essentially the same thing

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

    Dude increase your *FONT* size for tutorials at least 💡

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

    First comment

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

      autograph pls 😂

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

      🥇

  • @ahmatmhtlouky4179
    @ahmatmhtlouky4179 วันที่ผ่านมา

    Man😂 after each five words you link as to another video 🤦🏽‍♂️

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

    Too bad the jobs are all taken lol

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

    If you work with React you desperately need to understand closures. I have run into some insane React bugs that were created because of closures in complex functional React.

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

      HINT: Watch out for modifying an onChange when its getting prop drilled. Another reason to create context hooks as often as possible...

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

      Fact that this dude didn’t mention react once makes me question his understanding. Also the fact he messed up the for loop example.

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

    This is not true for all programming languages. JavaScript is an anomaly.

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

    god I hate javascript

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

    Why u move ur head too much? Can you hold your head still while recording or move less. I would appreciate it. The way u move your head makes me dizzy

  • @servertimeout8371
    @servertimeout8371 9 วันที่ผ่านมา

    no semicolons 😮 you would never get a job from me

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

    why your head bouncing so much? it's very disracting no matter how valuable content you show (always very valuable)

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

    Do a video on booty sizes

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

      What does that even mean?!!