0
0
Javascriptprogramming~20 mins

Event loop overview in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Event Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code snippet?

Consider the following JavaScript code. What will be printed to the console?

Javascript
console.log('Start');
setTimeout(() => console.log('Timeout'), 0);
Promise.resolve().then(() => console.log('Promise'));
console.log('End');
A
Start
Promise
End
Timeout
B
Start
End
Promise
Timeout
C
Start
End
Timeout
Promise
D
Start
Timeout
Promise
End
Attempts:
2 left
💡 Hint

Remember that Promise callbacks go to the microtask queue, which runs before the macrotask queue where setTimeout callbacks go.

Predict Output
intermediate
2:00remaining
What will this code print and why?

Analyze the following code and select the correct console output.

Javascript
console.log('1');
setTimeout(() => console.log('2'), 100);
setTimeout(() => console.log('3'), 0);
Promise.resolve().then(() => console.log('4'));
console.log('5');
A
1
5
3
4
2
B
1
5
2
3
4
C
1
4
5
3
2
D
1
5
4
3
2
Attempts:
2 left
💡 Hint

Microtasks run before any timers, even if the timer delay is zero.

Predict Output
advanced
2:30remaining
What is the output of this nested asynchronous code?

Look at this code with nested setTimeout and Promise. What will it print?

Javascript
console.log('A');
setTimeout(() => {
  console.log('B');
  Promise.resolve().then(() => console.log('C'));
}, 0);
Promise.resolve().then(() => {
  console.log('D');
  setTimeout(() => console.log('E'), 0);
});
console.log('F');
A
A
F
D
B
C
E
B
A
F
B
D
C
E
C
A
F
D
B
E
C
D
A
F
D
C
B
E
Attempts:
2 left
💡 Hint

Remember microtasks run immediately after the current task, before the next macrotask.

Predict Output
advanced
2:00remaining
What error does this code produce?

Consider this code snippet. What error will it raise when run?

Javascript
setTimeout(() => {
  throw new Error('Oops');
}, 0);

Promise.reject('Fail').catch(e => console.log('Caught:', e));
ACaught: Fail
BUncaught Error: Oops
CNo output, silent failure
DCaught: Oops
Attempts:
2 left
💡 Hint

Errors thrown inside setTimeout callbacks are not caught by Promise.catch.

🧠 Conceptual
expert
3:00remaining
Which option correctly describes the event loop phases order?

Which of the following lists the correct order of event loop phases in Node.js?

ATimers → Poll → Pending callbacks → Check → Close callbacks → Idle, prepare
BPoll → Timers → Check → Pending callbacks → Close callbacks → Idle, prepare
CTimers → Pending callbacks → Idle, prepare → Poll → Check → Close callbacks
DPending callbacks → Timers → Poll → Check → Close callbacks → Idle, prepare
Attempts:
2 left
💡 Hint

Recall the Node.js event loop phases as documented in official docs.