The JavaScript event loop enables asynchronous programming in a single-threaded environment by using a call stack for synchronous function execution, an event queue (FIFO structure) to hold callbacks from asynchronous operations, and a continuous loop that transfers callbacks from the queue to the call stack only when the stack is empty, allowing JavaScript to appear concurrent without true parallelism.
Understanding the JavaScript Event Loop: A 5-Minute Guide
Added:let's talk about one of the trickiest concepts in JavaScript which is the event Loop but trust me it's not as bad as you think so let's do it in about five minutes what's up everyone my name is James he quick and I do weekly videos about web development related topics and I talk a lot about JavaScript I've talked a lot about asynchronous JavaScript in the past and I want to tackle a pretty challenging concept maybe not as bad as you think which is the event Loop and node.js so I'm actually referencing an article here from Andrew who from the no doctors group on dev.2 I'll have a link to that content for you to go and check out as well which is the node.js visualized event Loop and I'm going to kind of use the video in here to kind of talk through how the event Loop Works in JavaScript so this has great little animated examples and I think that will be a great starting point all right so really quick if you want to get better at CSS and JavaScript I've got two courses that are now on sale Advent of CSS and then Advent of JS and these are each going to give you 24 challenges to practice your core CSS and JavaScript skills these are going to be on sale for 50 off with the coupon code black fry through Monday December 5th so if you go and check this out right now or in the next couple days you get 50 off the bundle of both Advent of CSS and Advent of JavaScript so the one thing I want to cover first is this idea of JavaScript being single threaded JavaScript at any time can only do one thing as opposed to other languages like C C plus plus Etc that can have multiple threads to do multiple things at the same time so you'll hear an important distinction in JavaScript where it can't run things in parallel it can't do two things at the same time it can do things concurrently which kind of lets them go on at the same time behind the scenes but then it processes those results at different times you can't process things at the same time so because of that the asynchronous nature with one single thread in JavaScript is very important where we don't want to block the the single thread that JavaScript depends on we want to offload the long standing task like making API calls and database calls somewhere else to be executed and that's what we're going to talk about today so in this example we have three lines of code we have a console log we have a database query that also has a callback function associated with it and then we have a subsequent console log so what happens inside of JavaScript is every time we call a function we throw that function onto the call stack now a stack is a data structure that is last in the first out so if we add something to the stack let's say a function and that function calls another function we add that to the stack add the next function that it calls to the stack Etc and we have to finish the top most item on the stack before we can go to the one below it so if we add one add another another finish this one pop it off finish this one pop it off finish the last one pop it off and now we have nothing left so that is what the call stack inside of node is it's keeping track of what functions are called and in what order to execute them so with the console log we go through we call this function we or we add it to the call stack we call it it executes because it's synchronous code and now it disappears from the call stack it changes though when we run this database query when we run this database query this is something that needs to run asynchronously so JavaScript wants to be able to initiate this process but then kick it off somewhere else to be handled asynchronously so that it doesn't block the main thread in JavaScript now that somewhere else is the lib UV API inside of node.js which is built with C plus plus and and does have the ability to run things asynchronously and in parallel so JavaScript takes in this function to the call stack it says hey this is something that's going to take some insignificant amount of time or some non-trivial amount of time needs to run asynchronously so we're going to dump this off and put that call inside of the libuv API now with that thing offloaded to the behind the scenes underlying apis JavaScript can continue to process the code so it's going to go down to the next function here which is another console log and do the same thing it did before add it to the call stack call that thing finish it and pop it off now again if it were to call another function you would stack them on top of each other and pop them off one at a time so how does JavaScript know when that behind the scenes asynchronous work is done well this is where the event queue becomes so important and this is where the event Loop and the admit queue come into play so what happens is after JavaScript offloads this asynchronous function to the libuv API the Libya UV API will do the processing and when it's done it's going to add that callback to the event queue now the event queue is a queue it's a data structure that is first in first out so think of it just like a q for a sandwich shop a line for tickets to get into a movie theater Etc the first one to come in is the first one to get served so as things are processed asynchronously in the libuv API as they're finished it's going to throw those callbacks onto the event queue and then the event Loop is a thing that's just constantly looking inside of the event queue to see if there's any new events that JavaScript needs to act on now JavaScript can't act on one of these these events until the actual call stack is empty so as long as the call stack is empty the event Loop will go and grab an event inside of the event queue and it will now propagate that up to the JavaScript layer into its call stack so that it can handle it just like any other callback function now if that callback is another asynchronous function it will then dump that thing back over to the back end or behind the scenes libuv API and go through that full cycle again otherwise if that callback is synchronous it will execute that thing and pop it off the call stack now this cycle continues to happen over and over JavaScript is going to process code it's going to add it to the call stock if it's synchronous it will execute and be done if it's async it will offload that to the libuv API and let it handle it asynchronously when it's done it will send the event to the event queue which is primarily a callback the event Loop is going to continue to search or look inside of the event queue and say if the call stack is empty I'm going to take the first event in the queue and throw that on throw that callback onto the call stack so this is what enables JavaScript although it is single threaded to appear to be able to run things asynchronously slash in parallel it's because it's offloading things to those lower level apis and then processing those events coming in when it can again JavaScript can only do one thing at a time it's single threaded but the behind the scenes lower level apis are what enable this asynchronous JavaScript or asynchronous nature of JavaScript to exist so that is the node.js event Loop in about five minutes I hope that made sense if you have additional questions let me know in the comments below make sure to go and follow no doctors and then the author of this specific article which is Andrew who to show some support for the content that they created and thank you to them for creating this animation which made the explanation of this a lot easier so hope you enjoyed the video I hope this gives you a better idea of the JavaScript event Loop let me know if you have comments questions anything you want in the comments below and I'll catch you next time
Up Next

TypeScript Fundamentals: Setup and Everyday Types for JavaScript Developers
@jherr
153.6K views•2021-04-19

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

HTTP Requests Explained: GET, POST, PUT, DELETE
@codecademy
103.1K views•2021-10-07

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science







![[JavaScript in ಕನ್ನಡ] - Functions in JavaScript](https://i.ytimg.com/vi/TyPREIFSNRM/maxresdefault.jpg)
JavaScript 2부](https://i.ytimg.com/vi/U34GhHn3oAs/maxresdefault.jpg)









![[BORA LER] Entendendo Algoritmos: Pilhas e stack call - Parte 07](https://i.ytimg.com/vi/EHZUB5DwrNE/maxresdefault.jpg)



















