Facebook Pixel
setTimeout Interview Questions in JavaScript

What is the output of console.log('1'); setTimeout(() => console.log('2'), 0); console.log('3') in JavaScript?

The answer

1, 3, 2. The timer callback goes to the macrotask queue and runs after the synchronous code finishes.

More FAQs in setTimeout Interview Questions in JavaScript

Because promise callbacks go to the microtask queue, which the event loop drains completely before touching the macrotask queue where setTimeout callbacks live.

3, 3, 3. var is function-scoped; all callbacks share one i, which is 3 by the time they run. Use let to fix this (logs 0, 1, 2).

No. The callback never runs before the delay. It may run later (if the call stack is busy or microtasks are queued), but never earlier than the delay.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.