Facebook Pixel

setTimeout Interview Questions in JavaScript

setTimeout is asked in many interviews. Here are the most common questions with answers and code.

setTimeout Interview Questions in JavaScript

setTimeout is a common interview topic, especially combined with closures and the event loop. Here are the most asked questions.

Q1: What is the output?

console.log("1"); setTimeout(() => console.log("2"), 0); console.log("3"); `` **Answer**: `1, 3, 2`. The timer callback goes to the macrotask queue and runs after the synchronous code. ### Q2: What is the output? ```js setTimeout(() => console.log("timer"), 0); Promise.resolve().then(() => console.log("promise")); `` **Answer**: `promise, timer`. Microtasks (promises) run before macrotasks (timers). ### Q3: What is the output? ```js for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 0); } `` **Answer**: `3, 3, 3`. `var` is function-scoped; all callbacks share one `i`. ### Q4: What is the output? ```js for (let i = 0; i < 3; i++) { setTimeout(() => console.log(i), 0); } `` **Answer**: `0, 1, 2`. `let` creates a fresh binding per iteration. ### Q5: What is the output? ```js for (var i = 0; i < 3; i++) { setTimeout((j) => console.log(j), 0, i); } `` **Answer**: `0, 1, 2`. The extra argument `i` is passed by value as `j`. ### Q6: What is the output? ```js setTimeout(() => console.log("a"), 0); setTimeout(() => console.log("b"), 0); setTimeout(() => console.log("c"), 0); `` **Answer**: `a, b, c`. Timers run in FIFO order (first in, first out) for the same delay. ### Q7: What is the output? ```js function foo() { setTimeout(() => console.log("inner"), 0); console.log("outer"); } foo(); `` **Answer**: `outer, inner`. The timer is queued; `"outer"` logs first. ### Q8: Does `setTimeout(cb, 0)` run `cb` immediately? **Answer**: No. It goes to the macrotask queue. The event loop runs it after the stack is empty and microtasks are done. ### Q9: Can `setTimeout` be early? **Answer**: No. The callback never runs before the delay. It may run later (if the stack is busy or microtasks are queued), but never earlier. ### Q10: How do you cancel a `setTimeout`? ```js const id = setTimeout(() => console.log("hi"), 1000); clearTimeout(id); // cancels it `` ### The Takeaway `setTimeout` interview questions test: synchronous vs async ordering, microtasks vs macrotasks, the loop closure bug (`var` vs `let`), extra arguments, FIFO ordering, and `clearTimeout`. Remember: microtasks run before macrotasks, `var` shares one `i`, `let` creates fresh bindings, and `setTimeout(cb, 0)` does not run immediately.

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

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.

Store the return value (a timer ID) and pass it to clearTimeout(id). This cancels the pending timer and the callback will not run.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.