What are event loop in JavaScript?
Asked on: Sep 12, 2023 12:40 PMWell kiddo, imagine you're at a party and you're the DJ. You have a big list of songs to play, but you can only play one song at a time, right? Now, let's say your friends start giving you more songs to add to your list. Even if they give you new songs while one is already playing, you don't stop the current song. You just add their song to the end of your list and play it when its turn comes up. That's kind of how JavaScript works!
JavaScript can only do one thing at a time (like playing a single song), but it has many things it needs to do (like your big list of songs). This is where the event loop comes in! The event loop is like our DJ friend who keeps track of all the tasks and runs them one by one.
The event loop checks if there are any tasks that need to be done, if there are, it does them one by one in order. If new tasks come in while it's busy (like new song requests at our party), they get added to the end of the line and will get done when their turn comes up.
So in simple terms for my little friend, an event loop is like a DJ at a party who plays songs (tasks) one after another without stopping or getting confused even when more song requests keep coming in!
Learn more about Event Loop here