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 method borrowing, and how do call and apply let you do it?
Method borrowing means using a method from one object on another by passing the other as this with call or apply. A classic case is Array.prototype.slice.call(arguments) on an array-like object.
IntermediateQuick answer1 min read - Which array methods change the original array and which return a new one?
push, pop, shift, unshift, splice, sort, reverse and fill change the original. map, filter, slice, concat, flat and the newer toSorted, toReversed and toSpliced return new arrays.
IntermediateQuick answer1 min read - Why does ['1', '2', '3'].map(parseInt) not give [1, 2, 3]?
map passes three arguments to the callback: item, index and array. parseInt uses the second as the radix, so it runs parseInt('2', 1) and parseInt('3', 2), both NaN. Use map(Number) or map(s => parseInt(s, 10)).
IntermediateQuick answer1 min read - 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 - 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