# Event Loop

> The loop that picks which waiting job runs next. It keeps one thread busy.

**Field:** Software Development  
**Category:** Programming Paradigms  
**Also known as:** JavaScript Event Loop, Run Loop

## Think of it like

Think of it like a waiter with one pair of hands. The waiter serves one table, then checks who is ready.

## Definition

The event loop is the part of a runtime that decides what code runs next. JavaScript runs on one thread, so only one piece of code runs at a time. Slow tasks are handed off, and their callbacks wait in a queue. Each time the current code finishes, the loop takes the next waiting job. Promise jobs go before timer jobs, which is why a 0 ms timer still runs last.

## Example

Jack logs 1, sets a 0 ms timer for 2, then logs 3. The page prints 1, 3, 2, because the timer waits for the queue.

## Example code

```javascript
console.log(1);
setTimeout(() => console.log(2), 0);
console.log(3);
// 1
// 3
// 2
```

The timer's job waits until the code in front has finished.

## Why it matters

It explains why a long loop freezes a page. It also shows why code may not run in the order written.

## Common mistake

Running a heavy loop on the main thread. Nothing else, not even clicks, can run until it ends.

## Three principles

- **One thing at a time.** On a single thread.
- **A queue of jobs.** Waiting their turn.
- **Promises first.** Then timers.

## Related terms

- [Asynchronous Code](https://learn.benibix.com/term/development/asynchronous-code)
- [Promise (JavaScript)](https://learn.benibix.com/term/development/promise)
- [Callback Function](https://learn.benibix.com/term/development/callback)
- [Node.js](https://learn.benibix.com/term/development/nodejs)

---

Source: https://learn.benibix.com/term/development/event-loop
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
