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 →
- 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