Member-only story
JavaScript Interview Prep: Event Loop
Why does Javascript have an event loop?
3 min readJun 21, 2024
- JavaScript is single threaded. It can only do 1 thing at a time.
- It can’t run things in parallel. It happens concurrently, meaning it processes things at different times.
- Javascript puts long standing tasks executed somewhere else.
What is a call stack?
- Every time Javascript calls a function, it throws that function into the call stack. A call stack is a data structure that is last in last out.
- Let’s say we now call db.query. First it goes into the call stack then it moves to the LIBUV API. So it can be handle asynchronously so it doesn’t block other call functions. It will now not block the main thread. LIBUV API can run asynchronously and in parallel. Now it’s ofloaded.
- Now the second console.log will be added to the call stack.