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 debounce and throttle? Implement both.
Debounce waits until calls stop for a set time and then runs the function once, like search-as-you-type. Throttle runs the function at most once per time window no matter how often it is called, like handling scroll events. Both are built with a closure that holds a timer or a timestamp.
IntermediateIn-depthCoding3 min read - Write a function that deep clones an object, including nested arrays and circular references.
Return primitives as they are. For objects and arrays, create an empty copy, record it in a WeakMap so circular references reuse it, then recursively clone each property. In modern runtimes, structuredClone does this for you.
IntermediateQuick answerCoding1 min read - Write a function that flattens a nested array of any depth.
Loop through the array; if an item is an array, flatten it recursively and add its items, otherwise add the item. The built-in shortcut is arr.flat(Infinity).
IntermediateQuick answerCoding1 min read - Write a memoize function that caches the results of a pure function.
Wrap the function and store results in a Map keyed by its arguments. If the key exists, return the cached value; otherwise compute, store and return. It only works safely for pure functions.
IntermediateQuick answerCoding1 min read - 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 - Write your own version of Function.prototype.bind.
Add a method on Function.prototype that remembers the original function, the chosen this and any preset arguments, and returns a new function that calls the original with apply, joining preset and new arguments.
IntermediateQuick answerCoding1 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