Interview Q&A / JavaScript
JavaScript interview questions
Closures, the event loop, this, promises and the core language questions every frontend round asks.
Prefer rapid revision? Practise JavaScript short interview answers →
- What is the difference between promises and async/await, and how do you handle errors with each?
async/await is cleaner syntax built on top of promises. An async function always returns a promise, and await pauses that function until a promise settles. With promises you handle errors using .catch(); with async/await you use try/catch around the await.
BeginnerIn-depth3 min read - 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 - What are the states of a Promise, and what happens if you resolve it twice?
A promise is pending, then becomes fulfilled with a value or rejected with a reason. Once settled it never changes, so later resolve or reject calls are ignored.
BeginnerQuick answer1 min read - What does an async function return?
Always a Promise. A returned value becomes the fulfilled value, and a thrown error becomes a rejection. That is true even when the function contains no await.
BeginnerQuick answer1 min read - How does promise chaining work, and what happens after a catch?
Every then returns a new promise, resolved with whatever the handler returns, so calls chain. A thrown error skips later then handlers until a catch, and after a catch that returns a value, the chain continues normally.
IntermediateQuick answer1 min read - 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 - Write your own version of Promise.all.
Return a new promise. Keep a results array and a counter. Wrap each input in Promise.resolve, store its value at its index, and resolve when the counter reaches zero. Reject as soon as any input rejects.
IntermediateQuick answerCoding1 min read