Closures Explained in 100 Seconds // Tricky JavaScript Interview Prep

Поділитися
Вставка
  • Опубліковано 29 лис 2024

КОМЕНТАРІ • 414

  • @theultimateshield5133
    @theultimateshield5133 3 роки тому +1052

    click the f**king button man its his f**king dream😂😁😂

  • @ihavetwofaces
    @ihavetwofaces 3 роки тому +251

    "If you ever find yourself faced with a question like this, the best thing you can do is talk through the code slowly and [demonstrate the knowledge you do have], so they know you're on the right track, even if you don't come to the right conclusion." Hands down the best tip for a technical interview of all time.
    I've assisted with coding interviews before, and the absolute worst thing you can do is shut up, say nothing, or say "I don't know." Even if you really don't know, show what you do know, ask questions, and try to apply what you have to the problem. That will earn you a tip or a hint or a rhetorical question, and that may lead you to the right answer. When I interviewed for my first job, I got two of three questions mostly wrong, but I was actively participating, asking questions, trying to work things through, etc., so I got the position.

    • @ko-Daegu
      @ko-Daegu 2 роки тому +5

      That doesn’t fully apply in todays world we’re competition is in all times high
      Talk thru your solution and get the right answer is now expected
      The curve to get an entry job got even steeper and it keeps getting this way

    • @Jubinmail
      @Jubinmail Рік тому

      Good tip. But don't expect a call back.

    • @jessh4016
      @jessh4016 Рік тому +4

      @@ko-Daegu I mean, of course being an expert in everything is the best. But if you don't know the full answer, talking through what you DO know is still infinitely better than just getting nervous and clamming up.

  • @Marcosmapf
    @Marcosmapf 3 роки тому +110

    About the question, it's also tricky that it prints 3 3 3 instead of 2 2 2 since the loop compares i to less than 3 instead of less or equal to 3, but it makes sense considering the i is incremented after the last loop (i++) and the last value evaluated is 3, which is false for the iteration but still a valid value for i

    • @adeshas83
      @adeshas83 3 роки тому

      It shouldn’t increment because the for condition has 2 rules that should prevent it from increments.

    • @migueljara9399
      @migueljara9399 2 роки тому +5

      Thank you for clarifying that. I was trying to wrap my head around it.

    • @ToddsDiscGolf
      @ToddsDiscGolf 2 роки тому +4

      Yes I spent a couple minutes staring at the problem on pause until I figured out why it printed 333 instead of 222.
      I’m glad I’m not the only one

    • @TsoiIzAlive
      @TsoiIzAlive Рік тому

      Tricky but not really tricky

    • @UnfamiliarPlace
      @UnfamiliarPlace 11 місяців тому +4

      Exactly -- if it stayed at 2 then i < 3 would remain true and the loop would go on infinitely -- the loop only ends because i has been incremented to 3. Understanding when the increment and check happen is key to understand loop conditions

  • @KoyFoster
    @KoyFoster 2 роки тому +102

    3 years writing JS and I never took the time to look up the diff between let and var. Thank you very much.

    • @dervaken3902
      @dervaken3902 2 роки тому +16

      nobody uses var nowadays lol

    • @Kivencito
      @Kivencito Рік тому

      @@dervaken3902 hi, i just started programming and i picked js for that. Would you please explain why is that? that nobody uses var? I use it quite often. Thanks!

    • @kaoticgamer1404
      @kaoticgamer1404 Рік тому +2

      @@Kivencito from what I know, it's better to use 'let' because if you use 'var', it can be easily changed by any function because it is global, and that can lead to unexpected results. It's better to use 'let' because it encapsulates your data and limits the scope, so that makes it easier to know what is affecting your variable and to know the outcome. Also use 'const' if you know the variable won't change.

    • @idc20627
      @idc20627 Рік тому +1

      @@dervaken3902 should still know what it is, what if you're working on legacy code. Problem is, most people are in it for the money and not the knowledge and then you wonder why bob knows so much and you know just enough to get a job and stay at that junior role for life.

    • @NoidoDev
      @NoidoDev Рік тому

      ​@@dervaken3902 😮 Why would you have your functions be able to access every variable outside of it?

  • @jkjoker777
    @jkjoker777 3 роки тому +208

    the trick question at the end would still log 3 three times even if the setTimeout time was set to zero. setTime is an api that does not run in the current js runtime

    • @Fireship
      @Fireship  3 роки тому +68

      Good point, I explain why that happens in the event loop in this video ua-cam.com/video/vn3tm0quoqE/v-deo.html

    • @omaribbrahim
      @omaribbrahim 3 роки тому +2

      I don’t think this is a trick question. I am new to Javascript and had to wreck my head to wrap it around this the first time it popped up in my code.

    • @okyanusozsahin3439
      @okyanusozsahin3439 3 роки тому

      Thank you!

    • @jkjoker777
      @jkjoker777 3 роки тому +21

      @@omaribbrahim that’s kinda the definition of a trick question ; non intuitive answer

    • @dhkatz_
      @dhkatz_ 3 роки тому +3

      Current JS runtime? That's some confusing wording. I think what you mean to say is that setTimeout will at minimum wait until the next tick of event loop plus whatever delay you set. Furthermore, if there are any other messages waiting to be processed by the loop (like other setTimeouts) then your callback must wait more. So a delay of 0 will just wait until the next event loop tick at minimum. Depending on what you're doing this could be far longer than 0 ms. The timeout is only the *minimum* guaranteed time.

  • @LoveThatSceneChannel
    @LoveThatSceneChannel 3 роки тому +153

    One day I will watch beyond the 100 seconds, and then I'll get closure

    • @MiguelQuinaRibeiro
      @MiguelQuinaRibeiro 3 роки тому +5

      This comment made me rethink my life 😱.

    • @xunxekri
      @xunxekri 3 роки тому

      Shame it's not on Clojure

    • @magne6049
      @magne6049 3 роки тому +1

      genius comment, multiple meanings, you are Christopher Nolan.

    • @LoveThatSceneChannel
      @LoveThatSceneChannel 3 роки тому +1

      @@magne6049 I see you 😂👍🏻

  • @devsauce
    @devsauce 3 роки тому +468

    Christian Bale === Closures. Got it Jeff.

    • @naythaniel
      @naythaniel 3 роки тому +9

      Christian Bale === actor
      Patrick Bateman === closures
      Christian Bale !== Patrick Bateman

    • @PickleSurpriseVEVO
      @PickleSurpriseVEVO 3 роки тому +16

      But American psycho ends without closure

    • @danieljoaquinsegoviacorona1734
      @danieljoaquinsegoviacorona1734 3 роки тому +3

      Whoever thought of closures is a psychopath for sure! lmao

    • @deepkhamaru1154
      @deepkhamaru1154 2 роки тому +1

      Well he did give closure to Batman! So... You're one hundred percent right!

    • @CarlJohnson-iv7sn
      @CarlJohnson-iv7sn 9 місяців тому

      Adding sigma Patrick Bateman with closures (hard interview topic) is such a chad ohio enjoyer thing to do. I'm sure Jeff was mewing while he made this video.

  • @kalvinpearce
    @kalvinpearce 3 роки тому +68

    JavaScript is a fever dream 👀
    Thanks for taking us further down the rabbit hole

    • @spythere
      @spythere 3 роки тому +2

      Just wait till you get to TypeScript...

    • @ДмитроПрищепа-д3я
      @ДмитроПрищепа-д3я 3 роки тому +7

      @@spythere typescript is less of a fever dream though

    • @spythere
      @spythere 3 роки тому +4

      @@ДмитроПрищепа-д3я Yeah, it's like a obscure dream but with some good aspects (like Intelisense or types) :D

  • @jordizweden5201
    @jordizweden5201 3 роки тому +111

    when you said "click the f*ckng button" you had me

  • @xrr-1
    @xrr-1 3 роки тому +15

    Another way to log 0,1,2 with `var` is by forming a closure using an IIFE
    A good way to solve if the interviewer doesn't allow you to use `let`

  • @pavanakumardr152
    @pavanakumardr152 3 роки тому +58

    you deserve more than 1 million subs 😊😊

  • @TheresAStarman21
    @TheresAStarman21 3 роки тому +102

    Last time I was this early, fireship was not using firebase.

    • @justingolden21
      @justingolden21 3 роки тому +9

      Hi Earl

    • @lars2299
      @lars2299 3 роки тому +1

      @@justingolden21 lol

    • @GavHern
      @GavHern 3 роки тому

      wasnt his name "Angular Firebase" before tho?

    • @TheresAStarman21
      @TheresAStarman21 3 роки тому

      fixed it.

    • @deebakapadia
      @deebakapadia 3 роки тому

      @@GavHern Yep. Then Google bought that domain. Idk how that happened lol

  • @gourabmukherjee2169
    @gourabmukherjee2169 3 роки тому +30

    This channel is my favourite.
    Best content + No bullshit = fireship ❤

  • @boiimcfacto2364
    @boiimcfacto2364 3 роки тому +3

    I've said this before, I'll say it again - YOU'RE THE BEST when it comes to these kinds of tutorials.

  • @milindsharma7184
    @milindsharma7184 3 роки тому +7

    My god !! In my 4 year experience it was first time I intentionally used closures. And the same day this video came out. Lucky day

    • @ChaoticNeutralMatt
      @ChaoticNeutralMatt 3 роки тому +1

      Curious what you used it for

    • @milindsharma7184
      @milindsharma7184 3 роки тому

      @@ChaoticNeutralMatt In simple words, I was trying to avoid infinite dependency loop in react useCallback hook

  • @hamdihadjkhlifa
    @hamdihadjkhlifa 3 роки тому +1

    I think if you r a teacher,your class don't need hours To understand anything, best channel

  • @Krzysiekoy
    @Krzysiekoy 2 роки тому +5

    Some people in the comment suggested that you could still use var and log 0, 1, 2 by doing IIFE (immediately invoked function expression) and that is a common solution when you are forced to use var (for example, during an interview).
    I've been coming back to this video from time to time as my understanding of JS improved, and I have found another interesting solution with var while exerimenting:
    for (var i = 0; i < 3; i++) {
    function log(){
    console.log(this);
    }
    setTimeout(log.bind(i), 100);
    }
    So, basically we create a bound log function, and we pass the value of i variable in the current iteration as "thisArg".

  • @muthukumar8009
    @muthukumar8009 3 роки тому +9

    Probably the best explanation ever for closure

    • @leoingson
      @leoingson 3 роки тому

      Yeah. But still I miss what it's actually good for. There is some form of protection for the inner function, but so what, why not just take arguments? Is it a workaround for incomplete language design of JS?

    • @jb5631
      @jb5631 2 роки тому

      @@leoingson memory / performance efficiency

  • @spaaaaace8952
    @spaaaaace8952 Рік тому

    What a legendary video. It took me around 10 minutes of pausing and rewinding just so I could absorb the information.

  • @bilalchami1
    @bilalchami1 3 роки тому +8

    How convenient. Was working on a memory optimization issue today related to this topic, and here you are nailing it and explaining everything in ~5mins
    Thanks 🤘🏼

  • @cyrilogoh6171
    @cyrilogoh6171 3 роки тому +2

    Finally understand how var and let work

  • @Aly_Fahmy
    @Aly_Fahmy 3 роки тому

    This is LITERALLY my favorite UA-cam channel right now!!!

  • @mayanxoni
    @mayanxoni 3 роки тому +5

    Man with this content, you deserve millions of subscribers!

  • @cryptodive7175
    @cryptodive7175 3 роки тому +1

    This is one of the best channels, short & to the point.

  • @bader6409
    @bader6409 3 місяці тому

    God bless you. I saw your vid on CRWD and now I look for you when I’m learning new JavaScript concepts from The Odin Project. You answer like how a perfect technical interview answer would be like and it’s helping me learn

  • @shivamjhaa
    @shivamjhaa 2 роки тому +2

    This is literally the best video explanation video on closures. I keep coming back to this. This explains why things we take for granted (for ex, debounce in lodash) works. Thanks a lot man.

  • @skylark.kraken
    @skylark.kraken 3 роки тому +5

    4:13 well, the event loop means that even if there was no delay it would still always run after i is 3. It's not a case of run it a million times and one won't be 3,3,3 due to timing, always 3,3,3.
    (also event loop in 100 seconds would help out a lot of people, understanding it solves a bunch of problems and I've seen people write in validations which would only fail if the event loop wasn't as it is)

  • @eyeofthetiger7
    @eyeofthetiger7 3 роки тому

    I was literally just reading about Javascript closures this morning for an optimization problem at work and then you post this

  • @arghyaguha6356
    @arghyaguha6356 Рік тому

    This is prolly the best explanation of closures there is!

  • @aspire.studios
    @aspire.studios 3 роки тому +2

    Even though I know enough js, I would love to watch an entire js series about every important concept just like this one.

  • @chigozie_jesse
    @chigozie_jesse 3 роки тому

    I got asked this question in my interview today. I am happy i finally got some closure

  • @Arcwise
    @Arcwise 3 роки тому +13

    Your presentations are truly awe-inspiring. Despite their short length, I‘m sure the more thought went into them to extract the bare essence for us to digest, with intentions to pique our interests and to motivate us to go in-depth on our own. The appreciation for your viewers valuable time is reflected though the content‘s succinctness and demonstrates self-awareness in this era of mind regurgitation incentivised by the algorithm.

  • @lirydasmaz9595
    @lirydasmaz9595 3 роки тому +2

    I'm pretty sure you will go beyond 1 million subscribers... no doubt.

  • @NFSprostreet98
    @NFSprostreet98 3 роки тому

    THIS IS THE BEST EXPLANATION FOR CLOSURE EVER! I WISHED THIS VIDEO WAS UPLOADED A FEW MONTHS AGO WHEN I WAS INTERVIEWING!!!! DAMNNNN

  • @clebhosh
    @clebhosh 3 роки тому +1

    i have never subscribed but watch your content. i just subscribed. you Sir are a brilliant mind

  • @DH-qu8wh
    @DH-qu8wh 3 роки тому +5

    I finally get it! Thanks for always having concise explanations of complex concepts!

  • @Computeshorts
    @Computeshorts 2 роки тому

    Been trying to understand closures for ages and this video taught me them in 5min well worth it. Thanks.

  • @toxaq
    @toxaq 3 роки тому +1

    If you’re ever asked a question like that, tell them you don’t want to work for a company that doesn’t value people.

  • @surlysmiles
    @surlysmiles 3 роки тому +1

    Love getting that interview question right. Makes me feel like it's time to move past js

  • @currymagc
    @currymagc 3 роки тому +25

    The heap is also for reference types, like objects and arrays, even in pure functions, but I suppose for this video that would be...out of scope 🤪

  • @PabloGnesutta
    @PabloGnesutta 3 роки тому

    Another way of solving the for loop problem without using let, but taking advantage of closures:
    for (var i = 0; i < 5; i++) {
    const log = (i) => {
    console.log(i)
    }
    setTimeout(log(i), 100);
    }
    when passing the i parameter to the log function expression, it gets encapsulated and preserve it's value even after the timeout stops...
    the log function will encapsulate the sate within the for loop.
    of course in this case it is much better to use Let, since that avoids polluting the global space, but this example is an illustration of the power of closures

  • @danvilela
    @danvilela 3 роки тому +1

    Best closure explanation i have ever seen! Good job

  • @sujaigorai7752
    @sujaigorai7752 3 роки тому +1

    Whenever an execution context is created a new scope is created...and when a function is declared inside the execution context the function hold the reference to that scope....but if no function is declared inside the execution context a new scope still created but it's immediately garbage collected after the end of execution...it's very simple concept

  • @minercreepmc
    @minercreepmc 3 роки тому

    I learned closure yesterday and you release this vid. Awesome

  • @TechdubberStudios
    @TechdubberStudios 3 роки тому +1

    believe it or not, I guessed that it will console log 3 for three times. But I didn't know that the behavior will change when using "let" instead of "var". There's always something new to learn, I guess.

  • @suraj-ram7488
    @suraj-ram7488 3 роки тому +1

    Love the concise teaching style

  • @esc120
    @esc120 3 роки тому +5

    And before 'let' was introduced, people had to use IIFE with 'i' as parameter to make it local.

  • @miku4j
    @miku4j 3 роки тому

    These 100 second series really awaken my motivation. Thank you

  • @samisbgh6253
    @samisbgh6253 3 роки тому +2

    Styled-Components in 100s 🔥🔥

  • @namanagarwal690
    @namanagarwal690 3 роки тому +4

    I don't how you do it but your content is amazing. You sometimes cover subtle details that anyone would overlook.

  • @ChrisAthanas
    @ChrisAthanas 3 роки тому

    The best explanation of a tricky subject that I've ever seen

  • @Pcoxproductions
    @Pcoxproductions 3 роки тому +1

    You'll get to a million its just a matter of time

  • @wintorez
    @wintorez 3 роки тому

    Thank you! This video saved me in a job interview today!

  • @svs6666
    @svs6666 2 роки тому

    Happy to see you achieve your childhood dream of 1 million subs. Your explanations are so succinct.

  • @bhaswarkashyap3069
    @bhaswarkashyap3069 3 роки тому +2

    I got this exact question today in an interview and guess what I failed miserably :(. Thanks Fireship for enhancing my storage of knowledge :)

  • @lelilimon
    @lelilimon 8 місяців тому

    2:02 it's 2024 and almost 3M subscribers. That equals to childhood dream fulfilled three times, congrats!

  • @whysoasync4933
    @whysoasync4933 3 роки тому +1

    This is the best channel on youtube!

  • @alexbhattaraicruze
    @alexbhattaraicruze 2 роки тому

    I recently gave an interview for a company. I was asked the same question. I wish I had watched this video before.

  • @devanshsinghparmar
    @devanshsinghparmar 3 роки тому +1

    He Deserve, trillions of subs

  • @fatgolem69
    @fatgolem69 3 роки тому +2

    700k Subs
    Let's Goooo Fireship

  • @palachokrab5711
    @palachokrab5711 3 роки тому +2

    Beautifull.. you worth 1 million subscribe

  • @theidk9567
    @theidk9567 Рік тому

    Took me an hour to fully understand a 5 minute video, but I got it

  • @Krzysiekoy
    @Krzysiekoy 3 роки тому +1

    If we move the log function outside of the loop (to the top of the script), the results are the same
    1. Why is the function log declared inside the loop? It this to add extra "trickiness" / "confusion" to the question? To make it more difficult?
    2. What happens with the log function during every iteration? It is assigned to a const variable, so I would assume you could not reassign it? Do we create a brand new, independent function during each iteration?
    Also, trying to answer my own question (2) about the const. Would this be a correct way of thinking?
    (code is modified just for the purpose of understanding)
    const funcArray = [];
    for (let i = 0; i < 3; i++) {
    const log = () => console.log(i);
    funcArray.push(log);
    }
    const log1 = funcArray[0]; // this is an independent function that "remembers" variable i being 0; thanks to closure
    const log2 = funcArray[1]; // this is an independent function that "remembers" variable i being 1; thanks to closure
    const log3 = funcArray[2]; // this is an independent function that "remembers" variable i being 2; thanks to closure
    log1(); // logs 0
    log2(); // logs 1
    log3(); // logs 2
    All if the i variables were recovered from the HEAP memory.
    Is the the right way of thinking about this?

  • @vaibhavdesai8455
    @vaibhavdesai8455 Рік тому

    Buy using Var also, we are able to print 0,1,2 but fr that we need to write another function, and put them inside in it, var is global variable, so they pointing to same memory location, that's the reason, so we just need to change that position, when everyone loop iterate, so for that we need closure, that definitely solve the problem.

  • @rylandbrough9616
    @rylandbrough9616 3 роки тому +5

    If you want to use a predefined variable without creating a closure, don’t forget about this:
    const add = (a, b = 3) => {
    return a + b;
    };

  • @imanhosseini6908
    @imanhosseini6908 2 роки тому +1

    OrganicChemistryTutor of programming u great ser

  • @13hdni37
    @13hdni37 3 роки тому +5

    I love your 100 seconds videos, especially when they go beyond 😊. Keep up the good work!

  • @talhaibnemahmud
    @talhaibnemahmud 3 роки тому +1

    You deserve way more than a million subscribers ❤️

  • @Stoney_Eagle
    @Stoney_Eagle 3 роки тому

    I just use let and const, saved me lots of headaches when I started learning.

  • @mattunlu8916
    @mattunlu8916 3 роки тому

    I like the way how you explain things in short time!

  • @JominJose
    @JominJose Рік тому

    This is the best video on closure

  • @seal0118
    @seal0118 3 роки тому

    impressive, very nice
    lets see paul allen's closure

  • @lesterdelacruz5088
    @lesterdelacruz5088 7 місяців тому

    Depends on how fast that code is processed. When the first timeout executes it's theoretically possible the for loop to still be executing.

  • @qzbnyv
    @qzbnyv 3 роки тому

    Jake Archibald and Surma do a great job of covering these subtleties of the scoping of for loops in one of their relatively recent HTTP 203 UA-cam videos / podcasts. Strong recommend.

  • @surge2284
    @surge2284 3 роки тому +3

    699k subs, Nice!

  • @amirdiafi
    @amirdiafi 2 роки тому +2

    It is not only about the closures here, the execution stack is matter when you use Timers such as setTimeout(); it'll be executing the last one in the stack, that's why the log is read as the last assignment. (try printing the index without setTimout fun, you will get the same result when using var or let).

  • @robertrocker
    @robertrocker 3 роки тому +2

    Okay, you convinced me. I clicked the f**king button

  • @pratheeks
    @pratheeks 3 роки тому +25

    2:09 I literally cried laughing

  • @SirDamatoIII
    @SirDamatoIII 2 роки тому

    Congrats again on making your childhood dream :-)

  • @fredhair
    @fredhair 3 роки тому

    If you want to learn more about stack / heap memory; learn C++. You don't get a garbage collector with C++ and you get to have full control over memory.. this is both a blessing and a curse; if you've ever had a memory leak somewhere then you know why it can be a curse. In general follow the good rules of memory allocation, RAII and use smart pointers.

  • @chektek
    @chektek 3 роки тому +9

    "A closure is a function combined with its lexical environment." Just makes sense. As for hoisting just stop using var 😂

    • @jkjoker777
      @jkjoker777 3 роки тому

      The var vs let for-loop issue has nothing to do with hoisting and everything to do with scoping.
      Not that simple with hoisting. Hoisting is a result of how the JS runtime processes a script, so hoisting, or reserving memory space for objects, will happen every time.
      Avoiding the pitfalls of hoisting by not using var is a solution to a not real problem.
      Hoisting is only a problem bcs JS will still attempt to run with bad code where other langs wouldn’t .
      The solution is to not use objects that haven’t been declared yet.

    • @jkjoker777
      @jkjoker777 3 роки тому

      But yeah I’m being a nerd-ass,
      Totally agree with your insight that this one sentence definition of closures really does say it all

  • @danielzaiser
    @danielzaiser 3 роки тому

    explaining your thought process is very good advice in general

  • @MerthanMerter
    @MerthanMerter 2 роки тому

    tbh 42.9% sub is pretty good. u deserve 100% tho, no doubt.

  • @ALXG
    @ALXG 3 роки тому +1

    Fireship you are awesome 👏
    (as always)

  • @RobertWildling
    @RobertWildling 3 роки тому +1

    Aside from the fascinating way you can explain I am always amazed and amused by the images/videos to use! :-D

  • @listgaus
    @listgaus 3 роки тому +1

    Superb closure for my closure understanding. love your f**kin vid. +1 subscribed :D

  • @Sk8nRock
    @Sk8nRock Рік тому

    If you ever find yourself faced with a question like this, the best thing you can do is to walk away and not waste your time with such interviews, because they might as well ask you what is the output of: "b" + "a" + +"a" + "a" and other "tricky" questions like this.

  • @1J03B
    @1J03B 2 роки тому

    3:28 I think you missed the point in this explanation. It's not just that `i` is local to the for loop. If that were all, then the closures would still all reference the same variable and print 3, 3, 3. The point is that for loops re-declare a new `i` every iteration and copy the value from the previous iteration's `i`. That means each closure will capture its own separate variable (named `i`) that each ended with a different value.

  • @stith_pragya
    @stith_pragya 11 місяців тому

    Thank You So Much for this wonderful video............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @DK-ox7ze
    @DK-ox7ze 3 роки тому

    Seems like closures store values by reference if it's an object or array. The following code prints 3, 3, 3 :
    for(let j = {a: 0}; j.a < 3; j.a++) {
    setTimeout(() => console.log(j.a), 100);
    }
    However, the following code prints 0, 1, 1. That's wierd because both increment() and view() have closure over inner variable. Since inner is a simple variable, the closures should contain its copy. But here we see that when we call view() again, it prints 1, and not 0. Which means it's referencing the same copy:
    function outer() {
    let inner = 0;
    return {
    increment: () => {
    inner++;
    console.log(inner);
    },
    view: () => console.log(inner)
    }
    }
    const closure = outer();
    closure.view();
    closure.increment();
    closure.view();

  • @rasulali1435
    @rasulali1435 3 роки тому

    Now you making me regret having sponsor block after knowing about that brilliant subscribe section.

  • @Youkakun
    @Youkakun 3 роки тому +1

    1:58 This famous trick question isn't a problem for modern developers who always avoid var in favor of let. It's rather a bad signal from an interviewer still using old code.

  • @Gringo0517
    @Gringo0517 3 роки тому

    This exact topic was the basis of the very first question in my interview last week. If only I was Fireship... Button f**cking clicked.

  • @melvinyit6859
    @melvinyit6859 3 роки тому +1

    I know is a beginner question but can you do 100 second and beyond on js callback?
    Thanks for all your high quality video.

  • @LucaFang
    @LucaFang 3 роки тому +1

    Wow, now I know what a closure really is. Thanks my man. You’re the best :)

  • @Nurutomo
    @Nurutomo 3 роки тому +1

    I didn't know this was called closure, i usually use this :)

  • @posivibez2094
    @posivibez2094 3 роки тому

    excellent explanation thank you! was so confused before

  • @adnanamin3666
    @adnanamin3666 3 роки тому +1

    "F***ing Button"! Why don't you guys subscribe?! I don't get it! 🙄
    That outro fire animation was 🔥! Learned about "HOISTING" before, now this one made it stronger in knowledge. 😍

  • @AnimusAgent
    @AnimusAgent 3 роки тому

    good thing I work with Angular and typescript

  • @paulwhiterabbit
    @paulwhiterabbit 3 роки тому

    *function () { }* and *() => { }* are different in how they handle closure when passed as an argument.
    Also, always use *const* first then *let* when you need to update them later. Having to use *var* is rare.