Thu. Jul 18th, 2019

Host your Website

Read and learn

FatCow Plan for $3.15/mo. only

Closures in JavaScript | Inside a loop, inner function and setTimeoout

1 min read

https://i.ytimg.com/vi/-xqJo5VRP4A/hqdefault.jpg


JavaScript closure fundamentals. Why do we use closures?, how closures work inside a block scope, inner function, callback function and setTimeouts?. How to solve some closure related issues in a for loop.

Please be my patreons on patreaon
* https://www.patreon.com/techsith

Follow me for technology updates
* https://facebook.com/techsith
* https://twitter.com/techsith1

Help me translate this video.
*
Note: use https://translate.google.com/ to translate this video to your language. Let me know once you do that so i can give you credit. Thank you in advance.

Please be my patreons on patreaon
* https://www.patreon.com/techsith

Follow me for technology updates
* https://facebook.com/techsith
* https://twitter.com/techsith1

Help me translate this video.
* http://www.youtube.com/timedtext_video?ref=share&v=-xqJo5VRP4A
Note: use https://translate.google.com/ to translate this video to your language. Let me know once you do that so i can give you credit. Thank you in advance.

Keywords: JS Closures, JS Closure, JavaScript Closures, JavaScript Closure, lexical Scope, inner function, IIFE, closure inside a loop. Interview Question.

50 thoughts on “Closures in JavaScript | Inside a loop, inner function and setTimeoout

  1. Sorry, even that I really respect your effort, now I got completely lost. I am coming from Java world and if this was not enough, your explanation caused even more confusion in my mind. I think, I need another tutorial, something like "javascript for established programmers".

  2. bro love love love love u … it took me time to understand closure loop but end of day … it is deep understanding knowledge and I am beginner so it's so useful for me …. liked shared …already subscribed

  3. I have a question for you. You are using arrow notation. However, if you declare the inner function using a function declaration, you won't get the same results:

    if(true) {
    let i = 1;
    function f() {
    console.log(i);
    }
    }
    f(); // logs 1

    If the inner function is changed to an arrow function, it logs:

    VM2215:7 Uncaught TypeError: f is not a function
    at <anonymous>:7:1

    Why the difference?

  4. Hi TechSith, hope you are doing good, but one question that irritates me in every single interview that is what is the purpose of closure, why and where we use it, if you just simply clarify that then this would help me a lot. Waiting for you answer.

  5. You explained it very well.
    Thank you

    But, I have few confusion-

    I can see the Closure Scope property only in JSFiddle but when I run the same code in Chrome locally I don't see that why?

    Well, I did a little investigation and now I can see that Closure property is available under [[Scopes]], when I execute my code inside onLoad or onDOMReady function. Could you please confirm and explain why it's behaving differently?

    Also, I created a fiddle: http://jsfiddle.net/kpr1m392/ to ask you one more question. My Question is:

    In Chrome dev console I see a Closure object like below when I do console.dir(addToL2_2);

    2: Closure (window.onload)

    name: "Arnab"

    But, I have not used the variable name inside my function anywhere. And I can see it'snot available if I remove the addTo() function code or move the code after the log statements.

    It would be very helpful if you could help me understanding this behavior.

  6. let is a godsend. While we will still see var used all over the place for years to come, new devs should use let whenever possible. I can just imagine a dev whose used to languages like java and python cringing at how var works.

  7. This couldn't be a coincidence that I already liked your video before watching it completely. Time of the video 1:17 seconds, video liked. I wish there was a super like button.

  8. What I don't get is, if the i is completely new every time due to block scoping, how come it is 1 instead of 0 the second loop iteration (and 2 the third loop iteration, etc)? If the for loop is new every time, how come it remembers the i++? Why isn't it just i = 0, then the function runs, and then i++ makes i = 1, but then the loop restarts and the i becomes "completely new", doesn't that mean that since you do let = 0 in the start of every loop, that i should be 0? Thanks a lot for any help! 🙂

  9. OmG this man is the man… I was using for loop with multiple queries inside it and several other callbacks, then at the end I had put a condition to check that the loop had finished, then finalize with a last update query..so the loop always ended before even the queries had finished processing what ta ef man!! it drove me crazy for days checked on stack overflow for solutions but those solutions were just too thick to understand so I found techsith, he explains it very well and made it look so easy. Thanks a lot of this. subscribed, liked and shared. The example with setTimeout() was a mind opener I was able to see the entire code universe, everything now made sense. wonderful work.

Comments are closed.

Copyright © All rights reserved. | Newsphere by AF themes.