Interview Q&A / JavaScript

INTERVIEW Q&A · 71 QUESTIONS

JavaScript interview questions

Closures, the event loop, this, promises and the core language questions every frontend round asks.

  1. How do tasks and microtasks work in the JavaScript event loop?

    JavaScript runs one piece of code at a time. After the current code finishes, it runs all waiting microtasks (like promise callbacks) before the next task (like a setTimeout callback).

    IntermediateIn-depth2 min read
  2. How can awaiting inside a loop make code slow, and how do you run async tasks in parallel?

    Each await pauses until that task finishes, so awaiting one after another runs them in sequence. Start all the promises first and await them together with Promise.all to run them in parallel.

    IntermediateQuick answer1 min read
  3. How do you handle errors with async/await, and why can try/catch miss some of them?

    Wrap await calls in try/catch. The catch only sees rejections that are awaited inside the try. A promise you forget to await, or return without awaiting, escapes the block.

    IntermediateQuick answer1 min read
  4. What is the difference between Promise.all, allSettled, race and any?

    all waits for every promise and rejects on the first failure. allSettled waits for all and reports each outcome. race settles with the first promise to settle. any fulfills with the first success and rejects only if all fail.

    IntermediateQuick answer1 min read
  5. Why does await inside forEach not wait for each iteration?

    forEach ignores the promises returned by an async callback, so it starts them all and moves on without waiting. Use for...of with await for sequential work, or Promise.all with map for parallel work.

    IntermediateQuick answer1 min read
  6. Write a retry function with exponential backoff for an async call.

    Loop up to a maximum number of attempts, awaiting the call in a try/catch. On failure, wait delay times 2 to the power of the attempt, then try again. After the last failure, throw the last error.

    IntermediateQuick answerCoding1 min read