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 you check whether a value is an array in JavaScript?

    Use Array.isArray(value). typeof returns "object" for arrays, and instanceof Array can fail for arrays created in another frame or realm.

    BeginnerQuick answer1 min read
  2. How do you remove duplicate values from an array?

    The shortest way is [...new Set(arr)], because a Set stores each value once and keeps insertion order. Filtering with indexOf also works but is slower. For objects, dedupe by a key using a Map.

    BeginnerQuick answerCoding1 min read
  3. How does Array.reduce work, and why should you give it an initial value?

    reduce walks the array carrying an accumulator: each callback call gets the accumulator and the current item and returns the next accumulator. Without an initial value, an empty array throws a TypeError.

    BeginnerQuick answer1 min read
  4. What is the difference between includes and indexOf for arrays?

    includes returns true or false and can find NaN. indexOf returns the position or -1, uses strict equality and can never find NaN. Use includes for a yes/no check and indexOf when you need the position.

    BeginnerQuick answer1 min read
  5. What is the difference between map and forEach?

    map returns a new array built from your callback's return values. forEach returns undefined and is only for side effects like logging. Use map to transform data and forEach to do something with each item.

    BeginnerQuick answer1 min read
  6. What is the difference between slice and splice?

    slice copies a section into a new array and leaves the original alone. splice changes the original array in place by removing, replacing or inserting items, and returns the removed items.

    BeginnerQuick answer1 min read
  7. When would you use filter, find, some and every on an array?

    filter returns all matches as an array, find returns the first match or undefined, some says whether at least one matches, and every says whether all match. some and every stop as soon as the answer is known.

    BeginnerQuick answer1 min read
  8. Why does [10, 9, 1].sort() give [1, 10, 9], and how do you sort numbers correctly?

    By default sort converts items to strings and compares them in dictionary order, so "10" comes before "9". Pass a comparator like (a, b) => a - b to sort numbers. Note that sort changes the original array.

    BeginnerQuick answer1 min read
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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