The Event Loop in Action
To understand how JavaScript handles multiple tasks without running them all at once, you need to know about the event loop. The event loop is a core part of JavaScript's concurrency model. JavaScript runs in a single thread, which means it can only execute one task at a time. However, web applications often need to perform actions like waiting for user input, making network requests, or running timers—these should not block other code from running. The event loop is the mechanism that allows JavaScript to appear to do many things at once, by managing the execution of synchronous code and deferring asynchronous tasks until the main thread is free.
script.js
index.html
In this code sample, you see three statements: a console.log for "Start", a setTimeout with a callback, and a console.log for "End". When this code runs, "Start" is printed first. The setTimeout schedules its callback to run after a delay (which is set to 0 milliseconds), but the callback is not executed immediately. Instead, the event loop puts the callback into the callback queue. The next line, console.log("End"), is executed right away.
After all the synchronous code finishes executing and the call stack is empty, the event loop checks the callback queue. It finds the setTimeout callback waiting and then moves it onto the call stack, allowing it to run. This is why "Timeout callback" is printed last, even though the delay was set to zero.
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Awesome!
Completion rate improved to 3.57
The Event Loop in Action
Veeg om het menu te tonen
To understand how JavaScript handles multiple tasks without running them all at once, you need to know about the event loop. The event loop is a core part of JavaScript's concurrency model. JavaScript runs in a single thread, which means it can only execute one task at a time. However, web applications often need to perform actions like waiting for user input, making network requests, or running timers—these should not block other code from running. The event loop is the mechanism that allows JavaScript to appear to do many things at once, by managing the execution of synchronous code and deferring asynchronous tasks until the main thread is free.
script.js
index.html
In this code sample, you see three statements: a console.log for "Start", a setTimeout with a callback, and a console.log for "End". When this code runs, "Start" is printed first. The setTimeout schedules its callback to run after a delay (which is set to 0 milliseconds), but the callback is not executed immediately. Instead, the event loop puts the callback into the callback queue. The next line, console.log("End"), is executed right away.
After all the synchronous code finishes executing and the call stack is empty, the event loop checks the callback queue. It finds the setTimeout callback waiting and then moves it onto the call stack, allowing it to run. This is why "Timeout callback" is printed last, even though the delay was set to zero.
Bedankt voor je feedback!