Closures in JavaScript | Inside a loop, inner function and setTimeoout
Vložit
- čas přidán 15. 04. 2018
- 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
* / techsith
Follow me for technology updates
* / techsith
* / techsith1
Help me translate this video.
*
Note: use 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
* / techsith
Follow me for technology updates
* / techsith
* / techsith1
Help me translate this video.
* czcams.com/users/timedtext_vide...
Note: use 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. - Věda a technologie
I can proudly say that watching your JavaScript videos changed my knowledge level from beginner to medium, I know JS is like an ocean. Waiting on more n more videos from you. Thanking you is not just enough though
Great to hear! I will keep putting more such videos.
This is the first visual example of showing how let is different from var. Thanks for sharing this!
Glad it was helpful!
I was always confused by the setTimeout with closures. Thank you for finally clearing that up for me!
I am glad you got it. Thanks for watching!
@@Techsithtube hello.... Can u really provide ur JavaScript tutorial to us, full course??? PLEASE...🙏🙏🙏
BTW LEMME TELL U I AM A BIG FAN OF URS... PLEASE PROVIDE ME UR CLASS DETAILS, REALLY WANNA LEARN FROM YOU SIR
Seriously, the best tutorial I've ever seen about closures :D
I felt Akshay Saini's closure video is the best
Have read tons of closure examples and tutorials and I feel like have had a good understanding of them, but no one really broke it down in the way you did with each loop iteration. That is really the key to understanding why things are happening as they are. Great tutorial -- all of your videos are awesome
Tim, I am glad that you leaned from it. Thanks for watching
I started learning JS because of techsith.
I am glad you did. Thanks for watching!
@@Techsithtube me too. JavaScript seems very much clear after your videos.
Thanks.Your effort deserves a bigggg like.
Every time I watch this video I'd like to like it again!
You're an awesome teacher!
Thanks man for explaining a setTimeout inside for a loop with a var. It's priceless 👌🏻
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.
Carlos, I am glad that you finally got it. Please check it out my video on async await, that can be very useful. Thanks for watching!
I saw your Javascript Closure tutorial ( Closures Explained ) from 2015 as well. Both of them are very good. Thank you for keeping both of them.
wow that var and let concept for closures gave me goosebumps, well done Mr. Patel :)
Woah, nice tutorial! I just found your channel and it is amazing. I didn't even feel those 16 minutes pass while watching. Thanks for making top notch content!
I am glad that you learn something . Thanks for watching!
Wow , what an explanation about 'Closures' ! clear and precise !! Thanks a lot techsith !
Awesome, I have been struggling to understand many concepts and your tutorials are life savers! :D
I am glad that they helped you . Thanks for watching!
This is the best tutorial ever I have came acrossed to learn Closure, difference between using var and let .
I was really looking out for the explanation to the problem which you explained in the later part of the video. Thank you so much for the best explanation.
Great stuff, now its clear picture, added some more good stuff, thank you
I'm glad you are going over this again dude. I was actually going to send you an email to convince you to do another. Thaks
Awesome, it's not the most complete video, but it explains a lot that most closure videos don't. Good job.
Your explanation is just out of this world! Thanks, man! You helped me a lot!
probably the best video about this theme i have found so far, extra , perfect :)
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
I am glad that you got it in the end. Keep learning! Thanks for subscribing!
you blew my mind with that example...thank you!
Best explanation of this concept I have seen. I always do this but never understood why it worked.
Thank you for making such high-quality content available to us for *free* .
Thanks! Please also create a video on different kind of loops and their differences.
every time whenever i see your videos something new is must in your videos what i don't know it's a real tech chennal
Hey Techsith, Thanks for taking the time do this video. This is a great explanation.
I am glad that it helped. Thanks for watching Emile.
I have watched a lot of JS tutorials, but I really understood it because of techsith! You are a gem my friend... Thank you!
Thanks for a nice comment. :)
Techsith- Genius , thanks for this video
Well explained sir... Thank you
Mind blown!
Great upload.
Love your videos!! They are so helpful. Thank you.
thanks for watching Lexi. Keep on learning!
Wow, great video and released on my birthday!
Happy Belated Birthday! :)
The best of the best! Thank you
Well explained, thanks for the nice tutorial.
That's the best video thumbnail I've seen on CZcams! One of the best on closures too.
Thanks you . I put lot of thought in each thumbnail .
Thank you, all of your videos are really good and better descriptive.
I advanced in JS and working in react and I just forget things, this is a great refresher
I am glad that you think so . Keep on learning!
The best tutorial I have seen ever.
First half summary:
"closure is a function which remembers its dependencies, so that it can be used outside of containing function." pretty simple...
Second half summary (after for loop):
it was about difference between var and let keywords. good example to explain scopes. but i din't understand how is related to closures. please explain that.
It demonstrates how the results of closure are changed by block-scope and function-scope.
You are right. He used closure to show the difference between var and let. var uses functional scope, so the variable 'i' in the closure of the set timeout function keeps getting updated (its the way var works). Let uses block scope, so the variable 'i' in the closure of the set timeout function does not get updated.
If you look closely, the settimeout function executes a callback at later point in time. And by later point means when the outer function has been already executed and it's variable, i, is not present in the lexical scope, But even then callback can access, value of i and that's the magic of closure.
with the var keyword in the set Timeout function, the function always updates to the new value of i. Each of the setTimeout functions update to the new value of i after the for loop is finished, because the I in the setTimeout has function scope, which means the variable is visible only inside the function. Where as with "Let" a new variable is used for each of the SetTimeout functions, so the I isnt updated to 3 at the end like all the others. Think of the run time of the for loop is much less than the setTimeout function.
This is a super good explanation. Thx.
Thanks alot techsith
Best tutorial about closures by far, Thank you.
Great examples, thanks for your time!
Great explanation with clear situations, thank you for sharing with us.
Great Teacher!!! Awesome tutorial!
Great video, as always! ;)
Well explained sir, hats off to you
Finally I understand this. Thank you so much ! 🙏🏻
Glad it helped!
i liked it before watching it even before you said to "like it before watching it", as i have watched few JS tutorials on your channel in last few hours ,, cant resist clicking the "Like" button and yeah i am a subscriber now,,, Thank you so much for your efforts, #RespectForYou
Please you be in good mood and keep uploading useful tutorial for us..Well explained ..thnks Sir :)
I've seen so many definitions for closures but never really understood it. This video cleared a lot of doubts for me. Thank you!
Glad it was helpful!
Great explanation of this problem.
Mind blown! Never would have thought to look at it that way
:) Thanks for watching Justin.
thank you for the very good tutorial
Hello, i have watched your videos and the way you explain the concepts are amazing.
Thank you Arturo for watching and awesome comment. :)
Nice that clear my concept of closure
In any of your tutos I learn some great trick. This console.dir thing is sweet. I'm still learning so I only know that much. Still I haven't seen it anywhere else and I've been doing quite a lot of reading and viewing these days. Good for you.
7:03 block function
7:39 closures only show used variables within specified block function (shown via console.dir)
8:13 articulating closure
Thanks for the timeline.
Amazing explanation.
Very good explanation of closure from a new perspective.
Amazing dude. Really thanks, it's so important to learn by theory, examples and then challenges
Glad you think so! Fundamentals would take you far.
Awesome man! keep doing good work. May God bless you and fulfill all your dreams.
Thanks for a kind comment
Your explanation is super sir
It's very useful tut.
Hi sir thanks for your hard work to share your knowledge in closure It helps me to understand more how to use closure..
very good teacher, the way you try to explain is awesome
Very helpful tutorial I don't know this differences. Thank you
your videos make learning super easy. Thanks man
Thanks a lot...man you cleared the my all the doubts about closures
Glad to hear that
Thank u Sir! 👏 block and functional scope concepts are also cleared now with closures! ♥️
Great 👍
wow dats the problem i was solving last night. animating two arrows within .active class slider. JS can make u sometimes crazy. thanks techsith
Two things are happening here. one is hoisting where the 'i' becomes a global variable because of the use of Var which has no block scoping. The second is how the stack treats WebApi callback functions such as setTimeout. Each time the stack encounters setTimeout callback functions, execution is deferred to avoid blocking the main thread. however, the for loop still runs each time updating the value of 'i' and creating the callback functions( execution is deferred until the stack is empty). when the call back functions eventually get in the stack, their reference of 'i' has long been set to the upper limit of the for..loop and they console log this upper value. Use of IIFEs or keyword let solves this issue.
Outstanding !!!
You are absolutely right, I get an interview last week with a closure inside a loop question hahaha
You are REAL master. Keep Teaching us with same passion .Take a bow bro
Thanks for the awesome comment Saida :)
Great tutorial
Great work
Great explanation by @
techsith . thanks you for save our time.
What an awesome tutorial
I started love JS because of techsith
I am glad to have inspired you . Keep learning!
I am really glad, I found your channel!
Thanks for watching Vishnuvardan!
Thank you Sir. You explanation and topics r very nice. It actually helps.
best tutorial keep inspiring us.
Great explanation!!! Thank you for your help!!!
Really awesome ....big thanks to you for such videos
clear and concise.
I really appreciate the way it is explained.
Thank you for the lessons
Your explanation is crystal clear !!!
I loved your video. You're doing a great job.
Simple Awesome. well explained TX
He has got guts and confidence , Salute !
Kshitiz, thanks for an awesome comment!
I really like your video + screen capture tool, which is it?
Super cool and i really loved it
your videos are such incredible js tutorial i have ever seen in youtube
Glad you like them! Thank for watching!
Good video! Thanks for sharing
well explained...thanks
great video, thanks.
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.